﻿using Module_Debug.Hardware;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.IO.Ports;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
//using System.Threading.Tasks;

namespace Module_Debug
{
    public partial class RM692Cx_ProjA : Form
    {
        private bool Listening = false;//是否没有执行完invoke相关操作   
        private bool IClosing = false;//是否正在关闭串口，执行Application.DoEvents，并阻止再次invoke  
        #region //==============CA310===============
        CaSDK Ca310Obj = new CaSDK();
        private double auto_lum;//
        private double auto_x;
        private double auto_y;
        private double auto_T;
        private double auto_duv;

        private void Msr_CA310()
        {
            try
            {
                if (rbt_ca410.Checked == true)
                {
                    CaSDKObj.CA410_Measure_Lxy();
                    auto_lum = CaSDKObj.Lv;
                    auto_x = CaSDKObj.sx;
                    auto_y = CaSDKObj.sy;
                }
                else //(rbt_ca310.Checked == true)
                {
                    MainForm.Msr_CASDK();
                    auto_lum = MainForm.CASDK_LUM;
                    auto_x = MainForm.CASDK_x;
                    auto_y = MainForm.CASDK_y;
                }
            }
            catch
            {
                textBox1.Text += ("CA310/410未校准或者镜头被遮挡或读数异常！\r\n");
                //textBox1.Focus();//获取焦点
                textBox1.Select(textBox1.TextLength, 0);//光标定位到文本最后
                textBox1.ScrollToCaret();//滚动到光标处
                return;
            }
        }
        CaSDK CaSDKObj = new CaSDK();
        private void btnCal_Click(object sender, EventArgs e)   //CA310连接校准
        {
            try
            {
                Tim1Ca310.Start();//显示提示310连接提示信息
                textBox1.Text += "CA310/410 连接校准中...\r\n";
                OLED_Show_Click(btnblack, e);
                Application.DoEvents();//重点，必须加上，否则提示信息不能按顺序显示出来
            }
            catch
            {
                Thread.Sleep(100);
            }
            try
            {
                if (rbt_ca410.Checked == true)
                {
                    CaSDKObj.AutoConnect();
                    CaSDKObj.SingleCalibration(0, byte.Parse(CA310_Channel.Text), double.Parse(CA410_Measure_Freq.Text));
                }
                else
                {
                    CaSDKObj.CA310_Connect();
                    CaSDKObj.CA310_Initial(byte.Parse(CA310_Channel.Text));
                }
                //btnCal_manual.Text = "Wait"; Application.DoEvents();
                btn_CAS200_Start.Enabled = true;
                CAS200_dataGridView.Enabled = true;
                btn_CAS200_Save.Enabled = true;
                btn_CAS200_MeasureOnce.Enabled = true;
                btn_CAS200_Clean.Enabled = true;
                btn_CAS200_Delect.Enabled = true;
                gBx_CAS200_Auto_Test.Enabled = true;
                btn_CAS200_Stop.Enabled = true;
                //btnCal_manual.Text = "0Cal";
            }
            catch (Exception EX)
            {
                MessageBox.Show("CA310/410 Connect Fail!!" + EX.Message);
            }
            //try
            //{
            //    //btnCal_manual.Text = "Wait";
            //    Ca310Obj.CA310_Connect();
            //    Ca310Obj.CA310_Initial(byte.Parse(MainForm.CA310_Channel.Text));

            //    btn_CAS200_Start.Enabled = true;
            //    CAS200_dataGridView.Enabled = true;
            //    btn_CAS200_Save.Enabled = true;
            //    btn_CAS200_MeasureOnce.Enabled = true;
            //    btn_CAS200_Clean.Enabled = true;
            //    btn_CAS200_Delect.Enabled = true;
            //    gBx_CAS200_Auto_Test.Enabled = true;
            //    btn_CAS200_Stop.Enabled = true;
            //    //btnCal_manual.Text = "0Cal";
            //}
            //catch (Exception EX)
            //{
            //    MessageBox.Show("CA310 Connect Fail!!" + EX.Message);
            //}
            //btnCal_manual.Text = "0C";
            btnCal_manual.BackColor = Color.Gray;
            //ca310info.Visible = false;
            //ca310info.Enabled = true;
            btnAutoGamma.Enabled = true;
            textBox1.Text += "CA310 连接完成...\r\n";
            btnSetRGBBase.Enabled = true;

        }
        private void Tim1Ca310_Tick(object sender, EventArgs e)
        {
            //textBox1.Text += "CA310 连接校准中...\r\n";
            //ca310info.Visible = true; //显示提示信息
            Tim1Ca310.Enabled = false;
        }
        private void btn_DisconnectCaX10_Click(object sender, EventArgs e)
        {
            //Ca310Obj.objCa.RemoteMode = 0;
        }
        #endregion

        #region //------CA410---------------
        CaSDK Ca41Obj = new CaSDK();
        public double Lv = 0.0;
        public double sx = 0.0;
        public double sy = 0.0;
        //string ca410Info = "";
        #endregion
        string vesa_bmp_path;

        private List<byte> buffer = new List<byte>(2048);//默认分配1/16页内存，并始终限制不允许超过   
        private byte[] Rec_tmp = new byte[5];//
        private byte[] Rec_tmp1 = new byte[1];//
        private byte[] Send_buf = new byte[1];
        private byte signal_mode;
        public byte start_trans_flag = 0;
        public byte continueflag = 0;
        //public byte RM19X_0xFE_XX = 0;
        //public byte RM300_0XFE_XX = 0;
        public byte RM350_0XFE_XX = 0;
        //public byte RM010_0XFE_XX = 0;
        //public byte CH13721_0XFE_XX = 0;
        //public byte UD61720_0XFE_XX = 0;
        public byte Seil_port_Find_flag = 0;
        private byte[] Driver_IC = new byte[1];
        private byte[] OTP_Time_Data = new byte[15];
        private byte[] MTPDoneFlag = new byte[21];
        private List<string> ImageType;
        private delegate void MonitorSerialPortChangeEventHandler();
        private MonitorSerialPortChangeEventHandler MonitorCom;
        private int serialPortStatus;
        private int count = 0;
        //------------------------------------------

        //public static int RM692CXCmdType;
        public byte RM2AX_0XFE_XX = 0;

        CRC16 CRCData = new CRC16();

        #region//=================================Timer1=检测是否通讯故障=======================
        private void T1_Tick(object sender, EventArgs e)
        {
            T1.Enabled = false;
            textBox1.Text = textBox1.Text + "通讯故障!\r\n";
            textBox1.Focus();//获取焦点
            textBox1.Select(textBox1.TextLength, 0);//光标定位到文本最后
            textBox1.ScrollToCaret();//滚动到光标处
        }
        //=================================Timer====end=========================
        #endregion
        #region  //串口数据处理
        private void SerialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)//Getting data from Comm Port
        {
            //if (MainForm.IClosing) return;
            try
            {
                Listening = true;//设置标记，说明我已经开始处理数据，一会儿要使用系统UI的。         
                int n = MainForm.comPort.BytesToRead;
                byte[] buf = new byte[n];
                //byte data;

                MainForm.comPort.Read(buf, 0, n);//读取缓冲数据     
                bool data_1_catched = false;//缓存记录数据是否捕获到    
                buffer.AddRange(buf);
                byte CRCH, CRCL;
                if (buffer.Count > 3)
                {
                    if (((buffer[0] == 0x2F) || (buffer[0] == 0x3F) || (buffer[0] == 0x9F)) && (Setting.TempRecord.cmdLen > 0))
                    {
                        if (buffer.Count >= Setting.TempRecord.cmdLen)
                        {
                            Rec_tmp = new byte[Setting.TempRecord.cmdLen];
                            //byte[] crcTemp = new byte[Setting.TempRecord.cmdLen];
                            //buffer.CopyTo(0, crcTemp, 0, Setting.TempRecord.cmdLen);

                            CRCData.CalculateCrc16(buf, out CRCH, out CRCL);
                            if (buffer[Setting.TempRecord.cmdLen - 2] != CRCH && buffer[Setting.TempRecord.cmdLen - 1] != CRCL)     //CRC校验出错
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, Setting.TempRecord.cmdLen);//复制一条完整数据到具体的数据缓存   
                                data_1_catched = true;
                                buffer.RemoveRange(0, buffer.Count);    //正确分析一条数据，从缓存中移除数据。 
                            }
                        }
                    }
                    else
                        buffer.RemoveRange(0, buffer.Count);
                }
                if (data_1_catched)
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        T1.Enabled = false; //停止超时计数

                        if (Rec_tmp[0] == 0x3F)
                        {
                            switch (Rec_tmp[1])
                            {
                                case 0x01: { if (Rec_tmp[4] == 0x00) textBox1.Text = textBox1.Text + "写寄存器\tOK！\r\n"; } break;
                                case 0x02:
                                    {
                                        ReadedFlag = true;
                                        int i;
                                        string tmp = "";
                                        for (i = 3; i <= (Rec_tmp[2] + 1); i++)
                                        {
                                            tmp = tmp + String.Format("{0:X2}", Rec_tmp[i]) + ",";//16进制
                                        }
                                        tmp = tmp + String.Format("{0:X2}", Rec_tmp[i]);

                                        RM692CX_Reg_Data.Text = tmp;
                                        textBox1.Text = textBox1.Text + "寄存器\t读 OK！\r\n";

                                    }
                                    break;
                                case 0x04:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                        {
                                            if ((Rec_tmp[3] & 0xf0) == 0x00) textBox1.Text = textBox1.Text + "红 Gamma数据\t写 OK！\r\n";
                                            else if ((Rec_tmp[3] & 0xf0) == 0x40) textBox1.Text = textBox1.Text + "绿 Gamma数据\t写 OK！\r\n";
                                            else if ((Rec_tmp[3] & 0xf0) == 0x80) textBox1.Text = textBox1.Text + "蓝 Gamma数据\t写 OK！\r\n";
                                        }
                                    }
                                    break;
                                case 0x03:
                                    {
                                        if (Rec_tmp[4] == 0x00) textBox1.Text = textBox1.Text + "写Gamma1 OTP\t写 OK！\r\n";
                                        else if (Rec_tmp[4] == 0x02) textBox1.Text = textBox1.Text + "写Gamma1 OTP\t写 ERROR！\r\n";
                                    }
                                    break;
                                //case 0x10:
                                //    {
                                //        if (Rec_tmp[3] == 0x00) textBox1.Text = textBox1.Text + "写Power OTP\t写 OK！\r\n";
                                //        else if (Rec_tmp[3] == 0x02) textBox1.Text = textBox1.Text + "写Power OTP\t写 ERROR！\r\n";
                                //    }
                                //    break;
                                case 0x06:
                                    {
                                        string tmp = "";
                                        tmp = String.Format("{0:X2}", Rec_tmp[4]);
                                        RM692CX_Time.Text = tmp;////~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~··
                                        textBox1.Text = textBox1.Text + "OTP\t读 OK！\r\n";
                                    }
                                    break;
                                case 0x09:
                                    {
                                        if (Rec_tmp[4] == 0x00) textBox1.Text = textBox1.Text + "Change Gray\t写 OK！\r\n";
                                        else if (Rec_tmp[4] == 0x02) textBox1.Text = textBox1.Text + "Change GrayP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x05:
                                    {
                                        if (Setting.TempRecord.RM692CxCmdType == 0x03) //red
                                        {
                                            {
                                                //RM692CX_R0.Value = Decimal.Parse((Rec_tmp[4] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                                RM692CX_R1.Value = Decimal.Parse((Rec_tmp[6] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                                RM692CX_R2.Value = Decimal.Parse((Rec_tmp[8] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[9].ToString());
                                                RM692CX_R3.Value = Decimal.Parse((Rec_tmp[10] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                                RM692CX_R4.Value = Decimal.Parse((Rec_tmp[12] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());
                                                RM692CX_R5.Value = Decimal.Parse((Rec_tmp[14] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                                RM692CX_R6.Value = Decimal.Parse((Rec_tmp[16] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                                RM692CX_R7.Value = Decimal.Parse((Rec_tmp[18] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[19].ToString());
                                                RM692CX_R8.Value = Decimal.Parse((Rec_tmp[20] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[21].ToString());
                                                RM692CX_R9.Value = Decimal.Parse((Rec_tmp[22] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[23].ToString());
                                                RM692CX_R10.Value = Decimal.Parse((Rec_tmp[24] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[25].ToString());
                                                RM692CX_R11.Value = Decimal.Parse((Rec_tmp[26] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[27].ToString());
                                                RM692CX_R12.Value = Decimal.Parse((Rec_tmp[28] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[29].ToString());
                                                RM692CX_R13.Value = Decimal.Parse((Rec_tmp[30] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[31].ToString());
                                                RM692CX_R14.Value = Decimal.Parse((Rec_tmp[32] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[33].ToString());
                                                RM692CX_R15.Value = Decimal.Parse((Rec_tmp[34] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[35].ToString());
                                                RM692CX_R16.Value = Decimal.Parse((Rec_tmp[36] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[37].ToString());
                                                RM692CX_R17.Value = Decimal.Parse((Rec_tmp[38] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[39].ToString());
                                                RM692CX_R18.Value = Decimal.Parse((Rec_tmp[40] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[41].ToString());
                                                RM692CX_R19.Value = Decimal.Parse((Rec_tmp[42] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[43].ToString());
                                                RM692CX_R20.Value = Decimal.Parse((Rec_tmp[44] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[45].ToString());
                                                RM692CX_R21.Value = Decimal.Parse((Rec_tmp[46] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[47].ToString());
                                                RM692CX_R22.Value = Decimal.Parse((Rec_tmp[48] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[49].ToString());
                                                RM692CX_R23.Value = Decimal.Parse((Rec_tmp[50] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[51].ToString());
                                                RM692CX_R24.Value = Decimal.Parse((Rec_tmp[52] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[53].ToString());
                                                RM692CX_R25.Value = Decimal.Parse((Rec_tmp[54] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[55].ToString());
                                                RM692CX_R26.Value = Decimal.Parse((Rec_tmp[56] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[57].ToString());
                                                RM692CX_R27.Value = Decimal.Parse((Rec_tmp[58] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[59].ToString());
                                                RM692CX_R28.Value = Decimal.Parse((Rec_tmp[60] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[61].ToString());
                                                RM692CX_R29.Value = Decimal.Parse((Rec_tmp[62] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[63].ToString());
                                                RM692CX_R30.Value = Decimal.Parse((Rec_tmp[64] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[65].ToString());
                                                //RM692CX_R31.Value = Decimal.Parse((Rec_tmp[66] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[67].ToString());
                                                //RM692CX_R32.Value = Decimal.Parse((Rec_tmp[68] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[69].ToString());
                                            }
                                            textBox1.Text = textBox1.Text + "红 Gamma数据\t读 OK！\r\n";
                                        }
                                        else if (Setting.TempRecord.RM692CxCmdType == 0x04) //green
                                        {
                                            {
                                                //RM692CX_G0.Value = Decimal.Parse((Rec_tmp[4] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                                RM692CX_G1.Value = Decimal.Parse((Rec_tmp[6] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                                RM692CX_G2.Value = Decimal.Parse((Rec_tmp[8] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[9].ToString());
                                                RM692CX_G3.Value = Decimal.Parse((Rec_tmp[10] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                                RM692CX_G4.Value = Decimal.Parse((Rec_tmp[12] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());
                                                RM692CX_G5.Value = Decimal.Parse((Rec_tmp[14] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                                RM692CX_G6.Value = Decimal.Parse((Rec_tmp[16] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                                RM692CX_G7.Value = Decimal.Parse((Rec_tmp[18] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[19].ToString());
                                                RM692CX_G8.Value = Decimal.Parse((Rec_tmp[20] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[21].ToString());
                                                RM692CX_G9.Value = Decimal.Parse((Rec_tmp[22] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[23].ToString());
                                                RM692CX_G10.Value = Decimal.Parse((Rec_tmp[24] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[25].ToString());
                                                RM692CX_G11.Value = Decimal.Parse((Rec_tmp[26] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[27].ToString());
                                                RM692CX_G12.Value = Decimal.Parse((Rec_tmp[28] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[29].ToString());
                                                RM692CX_G13.Value = Decimal.Parse((Rec_tmp[30] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[31].ToString());
                                                RM692CX_G14.Value = Decimal.Parse((Rec_tmp[32] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[33].ToString());
                                                RM692CX_G15.Value = Decimal.Parse((Rec_tmp[34] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[35].ToString());
                                                RM692CX_G16.Value = Decimal.Parse((Rec_tmp[36] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[37].ToString());
                                                RM692CX_G17.Value = Decimal.Parse((Rec_tmp[38] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[39].ToString());
                                                RM692CX_G18.Value = Decimal.Parse((Rec_tmp[40] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[41].ToString());
                                                RM692CX_G19.Value = Decimal.Parse((Rec_tmp[42] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[43].ToString());
                                                RM692CX_G20.Value = Decimal.Parse((Rec_tmp[44] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[45].ToString());
                                                RM692CX_G21.Value = Decimal.Parse((Rec_tmp[46] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[47].ToString());
                                                RM692CX_G22.Value = Decimal.Parse((Rec_tmp[48] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[49].ToString());
                                                RM692CX_G23.Value = Decimal.Parse((Rec_tmp[50] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[51].ToString());
                                                RM692CX_G24.Value = Decimal.Parse((Rec_tmp[52] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[53].ToString());
                                                RM692CX_G25.Value = Decimal.Parse((Rec_tmp[54] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[55].ToString());
                                                RM692CX_G26.Value = Decimal.Parse((Rec_tmp[56] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[57].ToString());
                                                RM692CX_G27.Value = Decimal.Parse((Rec_tmp[58] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[59].ToString());
                                                RM692CX_G28.Value = Decimal.Parse((Rec_tmp[60] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[61].ToString());
                                                RM692CX_G29.Value = Decimal.Parse((Rec_tmp[62] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[63].ToString());
                                                RM692CX_G30.Value = Decimal.Parse((Rec_tmp[64] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[65].ToString());
                                                //RM692CX_G31.Value = Decimal.Parse((Rec_tmp[66] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[67].ToString());
                                                //RM692CX_G32.Value = Decimal.Parse((Rec_tmp[68] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[69].ToString());
                                            }
                                            textBox1.Text = textBox1.Text + "绿 Gamma数据\t读 OK！\r\n";
                                        }
                                        else if (Setting.TempRecord.RM692CxCmdType == 0x05) //blue
                                        {
                                            {
                                                //RM692CX_B0.Value = Decimal.Parse((Rec_tmp[4] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                                RM692CX_B1.Value = Decimal.Parse((Rec_tmp[6] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                                RM692CX_B2.Value = Decimal.Parse((Rec_tmp[8] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[9].ToString());
                                                RM692CX_B3.Value = Decimal.Parse((Rec_tmp[10] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                                RM692CX_B4.Value = Decimal.Parse((Rec_tmp[12] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());
                                                RM692CX_B5.Value = Decimal.Parse((Rec_tmp[14] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                                RM692CX_B6.Value = Decimal.Parse((Rec_tmp[16] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                                RM692CX_B7.Value = Decimal.Parse((Rec_tmp[18] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[19].ToString());
                                                RM692CX_B8.Value = Decimal.Parse((Rec_tmp[20] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[21].ToString());
                                                RM692CX_B9.Value = Decimal.Parse((Rec_tmp[22] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[23].ToString());
                                                RM692CX_B10.Value = Decimal.Parse((Rec_tmp[24] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[25].ToString());
                                                RM692CX_B11.Value = Decimal.Parse((Rec_tmp[26] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[27].ToString());
                                                RM692CX_B12.Value = Decimal.Parse((Rec_tmp[28] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[29].ToString());
                                                RM692CX_B13.Value = Decimal.Parse((Rec_tmp[30] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[31].ToString());
                                                RM692CX_B14.Value = Decimal.Parse((Rec_tmp[32] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[33].ToString());
                                                RM692CX_B15.Value = Decimal.Parse((Rec_tmp[34] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[35].ToString());
                                                RM692CX_B16.Value = Decimal.Parse((Rec_tmp[36] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[37].ToString());
                                                RM692CX_B17.Value = Decimal.Parse((Rec_tmp[38] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[39].ToString());
                                                RM692CX_B18.Value = Decimal.Parse((Rec_tmp[40] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[41].ToString());
                                                RM692CX_B19.Value = Decimal.Parse((Rec_tmp[42] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[43].ToString());
                                                RM692CX_B20.Value = Decimal.Parse((Rec_tmp[44] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[45].ToString());
                                                RM692CX_B21.Value = Decimal.Parse((Rec_tmp[46] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[47].ToString());
                                                RM692CX_B22.Value = Decimal.Parse((Rec_tmp[48] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[49].ToString());
                                                RM692CX_B23.Value = Decimal.Parse((Rec_tmp[50] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[51].ToString());
                                                RM692CX_B24.Value = Decimal.Parse((Rec_tmp[52] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[53].ToString());
                                                RM692CX_B25.Value = Decimal.Parse((Rec_tmp[54] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[55].ToString());
                                                RM692CX_B26.Value = Decimal.Parse((Rec_tmp[56] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[57].ToString());
                                                RM692CX_B27.Value = Decimal.Parse((Rec_tmp[58] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[59].ToString());
                                                RM692CX_B28.Value = Decimal.Parse((Rec_tmp[60] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[61].ToString());
                                                RM692CX_B29.Value = Decimal.Parse((Rec_tmp[62] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[63].ToString());
                                                RM692CX_B30.Value = Decimal.Parse((Rec_tmp[64] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[65].ToString());
                                                //RM692CX_B31.Value = Decimal.Parse((Rec_tmp[66] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[67].ToString());
                                                //RM692CX_B32.Value = Decimal.Parse((Rec_tmp[68] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[69].ToString());
                                            }
                                            textBox1.Text = textBox1.Text + "蓝 Gamma数据\t读 OK！\r\n";
                                        }

                                    }
                                    break;
                                case 0x0B:
                                    {
                                        for (int i = 0; i < 6; i++)
                                        {
                                            OTP_Time_Data[i] = Rec_tmp[4 + i];
                                        }
                                        textBox1.Text = textBox1.Text + "读寄存器\tOK！\r\n";
                                    }
                                    break;
                                case 0x1B:
                                    {
                                        for (int i = 0; i < 21; i++)
                                        {
                                            MTPDoneFlag[i] = Rec_tmp[4 + i];
                                        }
                                        textBox1.Text = textBox1.Text + "MTP Done Falg Read Complete\r\n";
                                        MTPDoneFlagShow.Enabled = true;
                                    }
                                    break;
                                default: break;

                            }
                        }
                        ////---------------------------------------------------Demura----------------------------------------
                        else if ((Rec_tmp[0] == 0x2F))  //Demura//RM692CX
                        {
                            textBox1.Text = textBox1.Text + "RM692CX\r\n";
                            switch (Rec_tmp[1])
                            {
                                case 0x01:
                                    {
                                        textBox1.Text = textBox1.Text + "Demura RAM Enable\r\n";
                                    }
                                    break;
                                case 0x02:
                                    {
                                        textBox1.Text = textBox1.Text + "Demura RAM Disable\r\n";
                                    }
                                    break;
                                case 0x03:
                                    {
                                        if (Rec_tmp[4] == 0x80)
                                        {
                                            textBox1.Text = textBox1.Text + "Demura RAM write Flash complete ! \r\n";
                                        }
                                    }
                                    break;
                                case 0x04:
                                    {
                                        textBox1.Text = textBox1.Text + "Checksum: " + Rec_tmp[4].ToString("x") + "," + Rec_tmp[5].ToString("x") + "\r\n";
                                    }
                                    break;
                                case 0x05:
                                    {
                                        if (Rec_tmp[4] == 0x80)
                                        {
                                            textBox1.Text = textBox1.Text + "Flash Erase Success ! \r\n";
                                        }
                                    }
                                    break;
                                case 0x06:
                                    {
                                        textBox1.Text = textBox1.Text + "flash ID: " + Rec_tmp[4].ToString("x") + "," + Rec_tmp[5].ToString("x") + "," + Rec_tmp[6].ToString("x") + "\r\n";
                                    }
                                    break;
                                case 0x07:
                                    {
                                        textBox1.Text = textBox1.Text + "Demura function on \r\n";
                                    }
                                    break;
                                case 0x08:
                                    {
                                        textBox1.Text = textBox1.Text + "Demura function off \r\n";
                                    }
                                    break;
                                case 0x09:
                                    {
                                        textBox1.Text = textBox1.Text + "Demura OTP complete$ \r\n";
                                    }
                                    break;
                                default:
                                    break;
                            }
                        }
                        else if (Rec_tmp[0] == 0x9f)
                        {
                            textBox1.Text += "命令已执行" + "\r\n";
                        }
                        textBox1.Focus();//获取焦点
                        textBox1.Select(textBox1.TextLength, 0);//光标定位到文本最后
                        textBox1.ScrollToCaret();//滚动到光标处
                        if (textBox1.Lines.Length > 200) textBox1.Text = "";
                    }));
                }
            }
            finally
            {
                Listening = false;
            }

        }
        #endregion

        #region//===========LCD cfg function:OLED_Show_Click====OLED显示=================
        private void Pattern_Write(byte R_value, byte G_value, byte B_value)
        {
            numPatternR.Value = R_value;
            numPatternG.Value = G_value;
            numPatternB.Value = B_value;
            OLED_Show_Click(btnPatternW, null);
        }
        private void OLED_Show_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[25];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 25;
            Send_buf[0] = 0x0A; //LCD配置标志
            Send_buf[2] = 0x15; //数据长度
            switch (btnTmp.Name)
            {

                case "btnPatternW":                             //写入Pattern参数
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = byte.Parse(numPatternR.Value.ToString());
                        Send_buf[4] = byte.Parse(numPatternG.Value.ToString());
                        Send_buf[5] = byte.Parse(numPatternB.Value.ToString());
                    }
                    break;
                case "btnPrevPic": //Send_buf[1] = 0x1B; break; //上一张图片
                    {
                        Send_buf[1] = 0x1b;
                    }
                    break;
                case "btnNextPic": //Send_buf[1] = 0x1C; break; //下一张图片
                    {
                        Send_buf[1] = 0x1c;
                    }
                    break;
                case "btnred":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0xFF;
                        Send_buf[4] = 0;
                        Send_buf[5] = 0;
                    }
                    break; //R
                case "btngreen":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0;
                        Send_buf[4] = 0xFF;
                        Send_buf[5] = 0;
                    }
                    break; //G
                case "btnblue":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0;
                        Send_buf[4] = 0;
                        Send_buf[5] = 0xFF;
                    }
                    break; //B
                case "btnwhite":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0xFF;
                        Send_buf[4] = 0xFF;
                        Send_buf[5] = 0xFF;
                    }
                    break; //W
                case "btnblack":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0;
                        Send_buf[4] = 0;
                        Send_buf[5] = 0;
                    }
                    break; //BL


                case "btnPattern16":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 16;
                        Send_buf[4] = 16;
                        Send_buf[5] = 16;
                    }
                    break; //16灰阶

                case "btnPattern32":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 32;
                        Send_buf[4] = 32;
                        Send_buf[5] = 32;
                    }
                    break; //32灰阶

                case "btnPattern64":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 64;
                        Send_buf[4] = 64;
                        Send_buf[5] = 64;
                    }
                    break; //64灰阶

                case "btnPattern96":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 96;
                        Send_buf[4] = 96;
                        Send_buf[5] = 96;
                    }
                    break; //96灰阶

                case "btnPattern128":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 128;
                        Send_buf[4] = 128;
                        Send_buf[5] = 128;
                    }
                    break; //128灰阶
                case "btnPattern192":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 192;
                        Send_buf[4] = 192;
                        Send_buf[5] = 192;
                    }
                    break; //192灰阶

                case "btnPattern224":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 224;
                        Send_buf[4] = 224;
                        Send_buf[5] = 224;
                    }
                    break; //224灰阶
                default:
                    break;
            }

            CRCData.CalculateCrc16(Send_buf, out Send_buf[23], out Send_buf[24]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 25);
                return;
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            Thread.Sleep(800);
        }
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                numPatternG.Value = numPatternR.Value;
                numPatternB.Value = numPatternG.Value;
                numPatternG.Enabled = false;
                numPatternB.Enabled = false;
            }
            else
            {
                numPatternG.Enabled = true;
                numPatternB.Enabled = true;
            }
        }
        private void numPatternR_ValueChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                numPatternG.Value = numPatternR.Value;
                numPatternB.Value = numPatternR.Value;
            }
            OLED_Show_Click(btnPatternW, e);
            Thread.Sleep(2);
        }
        private void numPatternG_ValueChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                numPatternB.Value = numPatternG.Value;
                numPatternR.Value = numPatternG.Value;
            }
            OLED_Show_Click(btnPatternW, e);
            Thread.Sleep(2);
        }
        private void numPatternB_ValueChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                numPatternG.Value = numPatternB.Value;
                numPatternR.Value = numPatternB.Value;
            }
            OLED_Show_Click(btnPatternW, e);
            Thread.Sleep(2);
        }
        #endregion
        //public class MyUpDown : NumericUpDown
        //{     //重写NumericUpDown
        //    public override void UpButton()
        //    {
        //        decimal inc = this.Increment;
        //        if ((Control.ModifierKeys & Keys.Control) == Keys.Control) this.Increment *= 10;
        //        base.UpButton();
        //        this.Increment = inc;
        //    }
        //    // TODO: DownButton
        //}

        #region       //--------------------Demura 操作---------------       
        private void btnLocate_Demura_File_Click(object sender, EventArgs e)
        {
            //FolderBrowserDialog fldb = new FolderBrowserDialog();
            //fldb.ShowNewFolderButton = false;
            //DialogResult res = fldb.ShowDialog();
            //if (res != System.Windows.Forms.DialogResult.OK)
            //{
            //    MessageBox.Show("没选择任何文件.");
            //    return;
            //}
            //string path = fldb.SelectedPath;

            //if (!Directory.Exists(path))
            //{
            //    MessageBox.Show("文件夹不存在.");
            //    return;
            //}
            if (textBox5.Text != "")
            {
                defaultfilePath = textBox5.Text;
            }
            else
            {
                StreamReader streamReader = new StreamReader("./update/DemuraHexAddr.txt", Encoding.Default);
                defaultfilePath = streamReader.ReadLine();
                streamReader.Close();
            }
            FolderBrowserDialog fldb = new FolderBrowserDialog();
            //首次defaultfilePath为空，按FolderBrowserDialog默认设置（即桌面）选择
            if (defaultfilePath != "")
            {                //设置此次默认目录为上一次选中目录
                fldb.SelectedPath = defaultfilePath;
            }
            fldb.ShowNewFolderButton = false;
            DialogResult res = fldb.ShowDialog();
            if (res != System.Windows.Forms.DialogResult.OK)
            {
                MessageBox.Show("没选择任何文件.");
                return;
            }
            defaultfilePath = fldb.SelectedPath;//记录选中的目录
            path = fldb.SelectedPath;
            if (!Directory.Exists(path))
            {
                MessageBox.Show("文件夹不存在.");
                return;
            }

            listBox3_Demura_File.Items.Clear();
            string s = null;
            DirectoryInfo d = new DirectoryInfo(path);
            FileInfo[] Files = d.GetFiles("*.hex");

            // List<string> lstr = new List<string>();

            foreach (FileInfo file in Files)
            {
                s = file.Name;
                listBox3_Demura_File.Items.Add(s);
            }
            //bin文件 识别
            FileInfo[] Files1 = d.GetFiles("*.bin");
            foreach (FileInfo file in Files1)
            {
                s = file.Name;
                listBox3_Demura_File.Items.Add(s);
            }
            //bin文件 识别
            textBox5.Text = path;
            // -------------------------------------------------------------------------
            StreamWriter sw = null; //文件流
            sw = new StreamWriter("./update/DemuraHexAddr.txt", false, Encoding.Default);//Encoding.GetEncoding("UTF-8")
            sw.Write(defaultfilePath);
            sw.Dispose(); //释放资源
            sw.Close(); //关闭数据流
        }

        [DllImport("user32.dll")]
        public static extern int MessageBoxTimeoutA(IntPtr hWnd, string msg, string Caps, int type, int Id, int time);//引用DLL

        private void Demura_Click(object sender, EventArgs e)
        {
            //string[] RM67298_Enable_Flash_Control = new string[4] { "fe", "20", "f3", "02"};
            Button btnTmp = (Button)sender;
            Send_buf = new byte[10];
            Setting.TempRecord.cmdLen = 10;
            Send_buf[0] = 0x2F;
            Send_buf[2] = 0x05;
            Send_buf[3] = 0x13;//0x11:692Ax-6.088;  1:692Ax-KPQ//2:692C4
            Send_buf[7] = 0x3F;
            switch (btnTmp.Name)
            {
                case "Demura_Function_ON":
                    {
                        Send_buf[1] = 0x07;
                    }
                    break;
                case "Demura_Function_OFF":
                    {
                        Send_buf[1] = 0x08;
                    }
                    break;
                case "Inter_Demura_data":
                    {
                        //Send_buf[1] = 0x01;
                        string str = "";
                        StreamReader streamReader = new StreamReader("./x_Raydium/DemuraConfig/Demura_ram_en_ProjA.gcfg", Encoding.Default);
                        str = streamReader.ReadToEnd();
                        MainForm.txtProcessDrivercode(str, out string txtbaseClearOriginal, out string txtMCleatOriginal);
                        MainForm.SendCmd(0); //(0):低速； （4）高速
                        btnData2DemuraRam.Enabled = true;
                        return;
                    }
                //break;
                case "btnIintFlash":
                    {
                        //Send_buf[1] = 0x01;
                        string str = "";
                        if (rdoB1.Checked)
                        {
                            StreamReader streamReader = new StreamReader("./x_Raydium/DemuraConfig/InitFlash_B1.gcfg", Encoding.Default);
                            str = streamReader.ReadToEnd();
                        }
                        else
                        {
                            StreamReader streamReader = new StreamReader("./x_Raydium/DemuraConfig/InitFlash.gcfg", Encoding.Default);
                            str = streamReader.ReadToEnd();
                        }
                        MainForm.txtProcessDrivercode(str, out string txtbaseClearOriginal, out string txtMCleatOriginal);
                        MainForm.SendCmd(0); //(0):低速； （4）高速
                        return;
                    }
                case "Exit_Demura_data":
                    {
                        Send_buf[1] = 0x02;
                    }
                    break;
                case "Flash_Write_Demura_data":
                    {
                        textBox1.Text = "Flash 烧录中，请等待12S...";
                        //Application.DoEvents();
                        //Send_buf[1] = 0x03;
                        string str = "";
                        StreamReader streamReader = new StreamReader("./x_Raydium/DemuraConfig/write_Flash.gcfg", Encoding.Default);
                        str = streamReader.ReadToEnd();
                        MainForm.txtProcessDrivercode(str, out string txtbaseClearOriginal, out string txtMCleatOriginal);
                        MainForm.SendCmd(0); //(0):低速； （4）高速
                        return;
                    }
                //break;
                case "Flash_Check_Demura_data":
                    {
                        Send_buf[1] = 0x04;
                    }
                    break;
                case "Flash_Erase_Demura_data":
                    {
                        Send_buf[1] = 0x05;
                    }
                    break;
                case "Demura_Flash_Enable":
                    {
                        Send_buf[1] = 0x06;
                    }
                    break;
                case "Flash_Demura_OTP":
                    {
                        Send_buf[1] = 0x09;
                    }
                    break;
                default:
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[8], out Send_buf[9]);
            if (Send_buf[1] == 0x03) //烧录前预加载Demura文件
            {
                PreLoad_Demura_data_Click();
            }
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 10);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
                return;
            }
            if (Send_buf[1] == 0x03)//烧写时给出相应耗时提示
            {
                MessageBoxTimeoutA((IntPtr)0, "烧写预计需要耗时15S，请留意信息窗烧录完成提示...", "---Info---15S后本窗口自动关闭---", 0, 0, 19000);
            }
            else if (Send_buf[1] == 0x05)  //添加擦除等待提示
            {
                MessageBoxTimeoutA((IntPtr)0, "擦除中，大约需要15S，请稍后...", "---Info---15S后本窗口自动关闭---", 0, 0, 19000);// 直接调用  3秒后自动关闭 父窗口句柄没有直接用0代替
                //原型：
                //int MessageBoxTimeoutA(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType, WORD wLanguageId, DWORD dwMilliseconds);
                //int MessageBoxTimeoutW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType, WORD wLanguageId, DWORD dwMilliseconds)[1]
                //hwnd//消息框的拥有窗口。
                //lptext//消息框的内容。
                //lpCaption//消息框的标题。
                //uType//指定一个决定对话框的内容和行为的位标志集。取值参照函数MessageBox的参数uType。
                //wLanguageId//函数扩展，一般取0。
                //dwMilliseconds//消息框延迟关闭时间，单位：毫秒。
                //返回值
                //参照函数MessageBox的返回值。
                //如果超时，即用户未操作，消息框自动关闭，返回32000。
            }


        }
        private void PreLoad_Demura_data_Click()  //预加载Demura文件//private void PreLoad_Demura_data_Click(object sender, EventArgs e)  //预加载Demura文件
        {
            Int32 m_txlen = 0;
            Int32 Once_txlen = 0;
            Int32 File_Length, File_Length_tep;
            bool tx_en = true;
            byte[] TemFileContent = new byte[1024 * 100];
            char[] Acsii_array = new char[2];
            Int32 div = 0;

            div = Convert.ToInt16(MainForm.HACT.Value) * 3;
            System.Int32 ByteReaded;

            try
            {
                string FileName = textBox5.Text + '\\' + listBox3_Demura_File.SelectedItem.ToString();
                FileStream aFile = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                File_Length = Convert.ToInt32(aFile.Length);
                File_Length_tep = File_Length;
                //USB_Rx_Buffer[0]==BMP_FLAG)&&(USB_Rx_Buffer[1]==0x5b)&&(USB_Rx_Buffer[2]==0xb5)&&(USB_Rx_Buffer[3]==0xd9)&&(USB_Rx_Buffer[4]==0x9d))
                textBox1.Text += "设定 Demura Flag \r\n";
                try
                { //Demura 告诉下位机Demura数据搬运开始
                    TemFileContent[0] = 0xbd;
                    TemFileContent[1] = 0x5B;
                    TemFileContent[2] = 0xb5;
                    TemFileContent[3] = 0xd9;
                    TemFileContent[4] = 0x9d;
                    TemFileContent[5] = Convert.ToByte(File_Length >> 24);
                    TemFileContent[6] = Convert.ToByte((File_Length >> 16) & 0xff);
                    TemFileContent[7] = Convert.ToByte((File_Length >> 8) & 0xff);
                    TemFileContent[8] = Convert.ToByte(File_Length & 0xff);
                    MainForm.comPort.Write(TemFileContent, 0, 9);
                }
                catch
                {
                    MessageBox.Show("串口有误，请检查", "错误");
                    tx_en = false;
                    return;
                }

                while (tx_en)
                {
                    if (File_Length_tep > (File_Length % div))
                    {
                        Once_txlen = div;
                    }
                    else  //最后一笔
                    {
                        Once_txlen = Convert.ToInt32(aFile.Length % div);
                        tx_en = false;
                    }
                    aFile.Seek(0 + m_txlen, SeekOrigin.Begin);    //跳转
                    ByteReaded = aFile.Read(TemFileContent, 0, Once_txlen);
                    try
                    {
                        MainForm.comPort.Write(TemFileContent, 0, ByteReaded + 0);
                    }
                    catch
                    {
                        MessageBox.Show("串口有误，请检查", "错误");
                        tx_en = false;
                        break;
                    }
                    //tx_en = false;
                    m_txlen += ByteReaded;
                    progressBar2.Value = Convert.ToInt32((double)m_txlen / aFile.Length * 100);
                    label_flash.Text = progressBar2.Value.ToString() + "%";
                    Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                    File_Length_tep = File_Length_tep - div;
                }
                aFile.Close();
                //try  
                //{   //Demura 数据搬运结束
                //    TemFileContent[0] = 0x5b;                    
                //    MainForm.comPort.Write(TemFileContent, 0,1);
                //}
                //catch
                //{
                //    MessageBox.Show("串口有误，请检查", "错误");
                //    tx_en = false;
                //    return;
                //}
            }
            catch
            {
                MessageBox.Show("请选择文件夹", "错误");
            }
        }
        private void Flash_Write_Demura_data_MouseEnter(object sender, EventArgs e)
        {
            ToolTip p = new ToolTip();
            p.ShowAlways = true;
            p.SetToolTip(this.Flash_Write_Demura_data, "数据搬入DemuraRAM,然后写入Flash，请静待10S...");
        }

        private void Demura_Mouse_Click(object sender, MouseEventArgs e)//左键Demura ON,右键Demura_OFF
        {
            if (e.Button == MouseButtons.Left)
            {
                Demura_Click(Demura_Function_ON, e);
            }
            else
            {
                Demura_Click(Demura_Function_OFF, e);
            }
        }
        private void Demura_Function_ON_MouseEnter(object sender, EventArgs e)
        {
            ToolTip p = new ToolTip();
            p.ShowAlways = true;
            p.SetToolTip(this.Demura_Function_ON, "鼠标左键ON,右键OFF,Demura ON 快捷键 <");
        }
        private void Demura_Function_OFF_MouseEnter(object sender, EventArgs e)
        {
            ToolTip p = new ToolTip();
            p.ShowAlways = true;
            p.SetToolTip(this.Demura_Function_OFF, "Demura OFF 快捷键 >");
        }

        private void btnReInit_MouseEnter(object sender, EventArgs e)
        {
            ToolTip p = new ToolTip();
            p.ShowAlways = true;
            p.SetToolTip(this.btnReInit, "重新初始化LCD,但此按键偶尔会失效");
        }
        #endregion---------

        private void CAS200_Measure_Information(double Dgv_lum, double Dgv_x, double Dgv_y) //---显示亮度数据---
        {
            int Row_Accum = 0, Current_Index = 0, Row_flag = 0;

            DataGridViewRow Row1 = new DataGridViewRow();
            Row_Accum = this.CAS200_dataGridView.Rows.Count - 1;
            if (Row_Accum != 0)
            {
                Current_Index = this.CAS200_dataGridView.SelectedCells[0].RowIndex;
                if ((Current_Index != Row_Accum - 1) && (Current_Index != Row_Accum))
                {
                    Row_Accum = Current_Index;
                    Row_flag = 1;
                }
                else
                {
                    this.CAS200_dataGridView.Rows.Add(Row1);
                }
                if ((Row_Accum != 0) && (Row_flag != 1))
                {
                    if (Current_Index != Row_Accum)
                    {
                        this.CAS200_dataGridView.Rows[Row_Accum - 1].Selected = false;
                        this.CAS200_dataGridView.Rows[Row_Accum].Selected = true;
                    }
                }
            }
            else
            {
                this.CAS200_dataGridView.Rows.Add(Row1);
            }
            this.CAS200_dataGridView.Rows[Row_Accum].Cells[0].Value = Row_Accum + 1;//序号
            //Rows[x]为第x行,Cells[x]为第x列
            this.CAS200_dataGridView.Rows[Row_Accum].Cells[1].Value = Dgv_x.ToString("#0.0000");
            this.CAS200_dataGridView.Rows[Row_Accum].Cells[2].Value = Dgv_y.ToString("#0.0000");
            this.CAS200_dataGridView.Rows[Row_Accum].Cells[3].Value = Dgv_lum.ToString("#0.0000");
            if (Row_flag == 1)
            {
                Row_flag = 0;
                Row_Accum = this.CAS200_dataGridView.Rows.Count - 1;
                this.CAS200_dataGridView.Rows[Current_Index].Selected = false;
                this.CAS200_dataGridView.Rows[Row_Accum - 1].Selected = true;
            }
            CAS200_dataGridView.FirstDisplayedScrollingRowIndex = CAS200_dataGridView.RowCount - 1;
        }
        //private int adjust_num = 0;
        //private string adjust_class = null;

        //private double lum_off, x_off, y_off;
        private int index = 0;
        //private int flag1 = 1;
        //private int flag2 = 1;
        //private int flag3 = 1;
        private int adjust_Rnum = 0;
        private int adjust_Gnum = 0;
        private int adjust_Bnum = 0;
        private int adjust_l = 0;
        private int adjust_x = 0;
        private int adjust_y = 0;
        //private int step_l = 1;
        //private int step_m = 5;
        //private int step_h = 12;
        Gamma_Addr GAMMAs = new Gamma_Addr();
        private ushort[] gamma_r = new ushort[31];
        private ushort[] gamma_g = new ushort[31];
        private ushort[] gamma_b = new ushort[31];
        private double lum_off, x_off, y_off;
        private double R_x = 0.6756;//
        private double R_y = 0.3222;
        private double G_x = 0.2129;
        private double G_y = 0.7378;
        private double B_x = 0.1412;
        private double B_y = 0.0356;
        private int flag1 = 1;
        private int flag2 = 1;
        private int flag3 = 1;
        #region //-----------RM692CX_Start----------------------------------------
        #region //---Gamma调整---
        private double[,] D_lums;   //目标值-亮度拆分成RGB
        private double[] D_lum;     //目标值-白色
        private double D_x, D_y;    //记录预设色坐标
        private bool[] PointCheck = new bool[31];//记录绑点是否选定
        private static int[,] RGBValue = new int[33, 3];//记录绑点RGB寄存器值
        private int step_l = 1;
        private int step_m = 5;
        private int step_h = 12;
        private int step_hh = 16;
        bool array2value = false;   //记录值写入窗体控件(记录绑点RGB寄存器值)的标志位
        bool value2array = true;    //窗体控件值保存到记录标志位
        private void RGBValueSet(bool inoutflag, int index, byte rgb, byte extraInfo)    //把窗体控件上Gamma绑点寄存器值保存于数组，便于后续处理
        {
            if (inoutflag)
            {
                switch (index)
                {

                    case 0:
                        switch (rgb)
                        {
                            case 0:
                                RGBValue[index, 0] = (int)RM692CX_R30.Value;
                                break;
                            case 1:
                                RGBValue[index, 1] = (int)RM692CX_G30.Value;
                                break;
                            case 2:
                                RGBValue[index, 2] = (int)RM692CX_B30.Value;
                                break;
                        }
                        break;
                    case 1:
                        switch (rgb)
                        {
                            case 0:
                                RGBValue[index, 0] = (int)RM692CX_R29.Value;
                                break;
                            case 1:
                                RGBValue[index, 1] = (int)RM692CX_G29.Value;
                                break;
                            case 2:
                                RGBValue[index, 2] = (int)RM692CX_B29.Value;
                                break;
                        }
                        break;
                    case 2:
                        switch (rgb)
                        {
                            case 0:
                                RGBValue[index, 0] = (int)RM692CX_R28.Value;
                                break;
                            case 1:
                                RGBValue[index, 1] = (int)RM692CX_G28.Value;
                                break;
                            case 2:
                                RGBValue[index, 2] = (int)RM692CX_B28.Value;
                                break;
                        }
                        break;
                    case 3:
                        switch (rgb)
                        {
                            case 0:
                                RGBValue[index, 0] = (int)RM692CX_R27.Value;
                                break;
                            case 1:
                                RGBValue[index, 1] = (int)RM692CX_G27.Value;
                                break;
                            case 2:
                                RGBValue[index, 2] = (int)RM692CX_B27.Value;
                                break;
                        }
                        break;
                    case 4:
                        switch (rgb)
                        {
                            case 0:
                                RGBValue[index, 0] = (int)RM692CX_R26.Value;
                                break;
                            case 1:
                                RGBValue[index, 1] = (int)RM692CX_G26.Value;
                                break;
                            case 2:
                                RGBValue[index, 2] = (int)RM692CX_B26.Value;
                                break;
                        }
                        break;
                    case 5:
                        switch (rgb)
                        {
                            case 0:
                                RGBValue[index, 0] = (int)RM692CX_R25.Value;
                                break;
                            case 1:
                                RGBValue[index, 1] = (int)RM692CX_G25.Value;
                                break;
                            case 2:
                                RGBValue[index, 2] = (int)RM692CX_B25.Value;
                                break;
                        }
                        break;
                    case 6:
                        switch (rgb)
                        {
                            case 0:
                                RGBValue[index, 0] = (int)RM692CX_R24.Value;
                                break;
                            case 1:
                                RGBValue[index, 1] = (int)RM692CX_G24.Value;
                                break;
                            case 2:
                                RGBValue[index, 2] = (int)RM692CX_B24.Value;
                                break;
                        }
                        break;
                    case 7:
                        switch (rgb)
                        {
                            case 0:
                                RGBValue[index, 0] = (int)RM692CX_R23.Value;
                                break;
                            case 1:
                                RGBValue[index, 1] = (int)RM692CX_G23.Value;
                                break;
                            case 2:
                                RGBValue[index, 2] = (int)RM692CX_B23.Value;
                                break;
                        }
                        break;
                    case 8:
                        switch (rgb)
                        {
                            case 0:
                                RGBValue[index, 0] = (int)RM692CX_R22.Value;
                                break;
                            case 1:
                                RGBValue[index, 1] = (int)RM692CX_G22.Value;
                                break;
                            case 2:
                                RGBValue[index, 2] = (int)RM692CX_B22.Value;
                                break;
                        }
                        break;
                    case 9:
                        switch (rgb)
                        {
                            case 0:
                                RGBValue[index, 0] = (int)RM692CX_R21.Value;
                                break;
                            case 1:
                                RGBValue[index, 1] = (int)RM692CX_G21.Value;
                                break;
                            case 2:
                                RGBValue[index, 2] = (int)RM692CX_B21.Value;
                                break;
                        }
                        break;
                    case 10:
                        switch (rgb)
                        {
                            case 0:
                                RGBValue[index, 0] = (int)RM692CX_R20.Value;
                                break;
                            case 1:
                                RGBValue[index, 1] = (int)RM692CX_G20.Value;
                                break;
                            case 2:
                                RGBValue[index, 2] = (int)RM692CX_B20.Value;
                                break;
                        }
                        break;
                    case 11:
                        switch (rgb)
                        {
                            case 0:
                                RGBValue[index, 0] = (int)RM692CX_R19.Value;
                                break;
                            case 1:
                                RGBValue[index, 1] = (int)RM692CX_G19.Value;
                                break;
                            case 2:
                                RGBValue[index, 2] = (int)RM692CX_B19.Value;
                                break;
                        }
                        break;
                    case 12:
                        switch (rgb)
                        {
                            case 0:
                                RGBValue[index, 0] = (int)RM692CX_R18.Value;
                                break;
                            case 1:
                                RGBValue[index, 1] = (int)RM692CX_G18.Value;
                                break;
                            case 2:
                                RGBValue[index, 2] = (int)RM692CX_B18.Value;
                                break;
                        }
                        break;
                    case 13:
                        switch (rgb)
                        {
                            case 0:
                                RGBValue[index, 0] = (int)RM692CX_R17.Value;
                                break;
                            case 1:
                                RGBValue[index, 1] = (int)RM692CX_G17.Value;
                                break;
                            case 2:
                                RGBValue[index, 2] = (int)RM692CX_B17.Value;
                                break;
                        }
                        break;
                    case 14:
                        switch (rgb)
                        {
                            case 0:
                                RGBValue[index, 0] = (int)RM692CX_R16.Value;
                                break;
                            case 1:
                                RGBValue[index, 1] = (int)RM692CX_G16.Value;
                                break;
                            case 2:
                                RGBValue[index, 2] = (int)RM692CX_B16.Value;
                                break;
                        }
                        break;
                    case 15:
                        switch (rgb)
                        {
                            case 0:
                                RGBValue[index, 0] = (int)RM692CX_R15.Value;
                                break;
                            case 1:
                                RGBValue[index, 1] = (int)RM692CX_G15.Value;
                                break;
                            case 2:
                                RGBValue[index, 2] = (int)RM692CX_B15.Value;
                                break;
                        }
                        break;
                    case 16:
                        switch (rgb)
                        {
                            case 0:
                                RGBValue[index, 0] = (int)RM692CX_R14.Value;
                                break;
                            case 1:
                                RGBValue[index, 1] = (int)RM692CX_G14.Value;
                                break;
                            case 2:
                                RGBValue[index, 2] = (int)RM692CX_B14.Value;
                                break;
                        }
                        break;
                    case 17:
                        switch (rgb)
                        {
                            case 0:
                                RGBValue[index, 0] = (int)RM692CX_R13.Value;
                                break;
                            case 1:
                                RGBValue[index, 1] = (int)RM692CX_G13.Value;
                                break;
                            case 2:
                                RGBValue[index, 2] = (int)RM692CX_B13.Value;
                                break;
                        }
                        break;
                    case 18:
                        switch (rgb)
                        {
                            case 0:
                                RGBValue[index, 0] = (int)RM692CX_R12.Value;
                                break;
                            case 1:
                                RGBValue[index, 1] = (int)RM692CX_G12.Value;
                                break;
                            case 2:
                                RGBValue[index, 2] = (int)RM692CX_B12.Value;
                                break;
                        }
                        break;
                    case 19:
                        switch (rgb)
                        {
                            case 0:
                                RGBValue[index, 0] = (int)RM692CX_R11.Value;
                                break;
                            case 1:
                                RGBValue[index, 1] = (int)RM692CX_G11.Value;
                                break;
                            case 2:
                                RGBValue[index, 2] = (int)RM692CX_B11.Value;
                                break;
                        }
                        break;
                    case 20:
                        switch (rgb)
                        {
                            case 0:
                                RGBValue[index, 0] = (int)RM692CX_R10.Value;
                                break;
                            case 1:
                                RGBValue[index, 1] = (int)RM692CX_G10.Value;
                                break;
                            case 2:
                                RGBValue[index, 2] = (int)RM692CX_B10.Value;
                                break;
                        }
                        break;
                    case 21:
                        switch (rgb)
                        {
                            case 0:
                                RGBValue[index, 0] = (int)RM692CX_R9.Value;
                                break;
                            case 1:
                                RGBValue[index, 1] = (int)RM692CX_G9.Value;
                                break;
                            case 2:
                                RGBValue[index, 2] = (int)RM692CX_B9.Value;
                                break;
                        }
                        break;
                    case 22:
                        switch (rgb)
                        {
                            case 0:
                                RGBValue[index, 0] = (int)RM692CX_R8.Value;
                                break;
                            case 1:
                                RGBValue[index, 1] = (int)RM692CX_G8.Value;
                                break;
                            case 2:
                                RGBValue[index, 2] = (int)RM692CX_B8.Value;
                                break;
                        }
                        break;
                    case 23:
                        switch (rgb)
                        {
                            case 0:
                                RGBValue[index, 0] = (int)RM692CX_R7.Value;
                                break;
                            case 1:
                                RGBValue[index, 1] = (int)RM692CX_G7.Value;
                                break;
                            case 2:
                                RGBValue[index, 2] = (int)RM692CX_B7.Value;
                                break;
                        }
                        break;
                    case 24:
                        switch (rgb)
                        {
                            case 0:
                                RGBValue[index, 0] = (int)RM692CX_R6.Value;
                                break;
                            case 1:
                                RGBValue[index, 1] = (int)RM692CX_G6.Value;
                                break;
                            case 2:
                                RGBValue[index, 2] = (int)RM692CX_B6.Value;
                                break;
                        }
                        break;
                    case 25:
                        switch (rgb)
                        {
                            case 0:
                                RGBValue[index, 0] = (int)RM692CX_R5.Value;
                                break;
                            case 1:
                                RGBValue[index, 1] = (int)RM692CX_G5.Value;
                                break;
                            case 2:
                                RGBValue[index, 2] = (int)RM692CX_B5.Value;
                                break;
                        }
                        break;
                    case 26:
                        switch (rgb)
                        {
                            case 0:
                                RGBValue[index, 0] = (int)RM692CX_R4.Value;
                                break;
                            case 1:
                                RGBValue[index, 1] = (int)RM692CX_G4.Value;
                                break;
                            case 2:
                                RGBValue[index, 2] = (int)RM692CX_B4.Value;
                                break;
                        }
                        break;
                    case 27:
                        switch (rgb)
                        {
                            case 0:
                                RGBValue[index, 0] = (int)RM692CX_R3.Value;
                                break;
                            case 1:
                                RGBValue[index, 1] = (int)RM692CX_G3.Value;
                                break;
                            case 2:
                                RGBValue[index, 2] = (int)RM692CX_B3.Value;
                                break;
                        }
                        break;
                    case 28:
                        switch (rgb)
                        {
                            case 0:
                                RGBValue[index, 0] = (int)RM692CX_R2.Value;
                                break;
                            case 1:
                                RGBValue[index, 1] = (int)RM692CX_G2.Value;
                                break;
                            case 2:
                                RGBValue[index, 2] = (int)RM692CX_B2.Value;
                                break;
                        }
                        break;
                    case 29:
                        switch (rgb)
                        {
                            case 0:
                                RGBValue[index, 0] = (int)RM692CX_R1.Value;
                                break;
                            case 1:
                                RGBValue[index, 1] = (int)RM692CX_G1.Value;
                                break;
                            case 2:
                                RGBValue[index, 2] = (int)RM692CX_B1.Value;
                                break;
                        }
                        break;

                    default: break;

                }
            }
            else
            {
                switch (index)
                {

                    case 0:
                        switch (rgb)
                        {
                            case 0:
                                RM692CX_R30.Value = RGBValue[index, 0];
                                break;
                            case 1:
                                RM692CX_G30.Value = RGBValue[index, 1];
                                break;
                            case 2:
                                RM692CX_B30.Value = RGBValue[index, 2];
                                break;
                        }
                        if ((extraInfo & 1) == 0x01)  //可调，但该绑点R/G/B插值迭代
                        {
                            RM692CX_R30.BackColor = Color.FromArgb(51, 136, 255);
                            RM692CX_G30.BackColor = Color.FromArgb(51, 136, 255);
                            RM692CX_B30.BackColor = Color.FromArgb(51, 136, 255);
                        }
                        else if ((extraInfo & 2) == 0x02)//"调不出，该绑点R/G/B插值迭代
                        {
                            RM692CX_R30.BackColor = Color.FromArgb(255, 169, 83);
                            RM692CX_G30.BackColor = Color.FromArgb(255, 169, 83);
                            RM692CX_B30.BackColor = Color.FromArgb(255, 169, 83);
                        }
                        break;
                    case 1:
                        switch (rgb)
                        {
                            case 0:
                                RM692CX_R29.Value = RGBValue[index, 0];
                                break;
                            case 1:
                                RM692CX_G29.Value = RGBValue[index, 1];
                                break;
                            case 2:
                                RM692CX_B29.Value = RGBValue[index, 2];
                                break;
                        }

                        break;
                    case 2:
                        switch (rgb)
                        {
                            case 0:
                                RM692CX_R28.Value = RGBValue[index, 0];
                                break;
                            case 1:
                                RM692CX_G28.Value = RGBValue[index, 1];
                                break;
                            case 2:
                                RM692CX_B28.Value = RGBValue[index, 2];
                                break;
                        }

                        break;
                    case 3:
                        switch (rgb)
                        {
                            case 0:
                                RM692CX_R27.Value = RGBValue[index, 0];
                                break;
                            case 1:
                                RM692CX_G27.Value = RGBValue[index, 1];
                                break;
                            case 2:
                                RM692CX_B27.Value = RGBValue[index, 2];
                                break;
                        }

                        break;
                    case 4:
                        switch (rgb)
                        {
                            case 0:
                                RM692CX_R26.Value = RGBValue[index, 0];
                                break;
                            case 1:
                                RM692CX_G26.Value = RGBValue[index, 1];
                                break;
                            case 2:
                                RM692CX_B26.Value = RGBValue[index, 2];
                                break;
                        }

                        break;
                    case 5:
                        switch (rgb)
                        {
                            case 0:
                                RM692CX_R25.Value = RGBValue[index, 0];
                                break;
                            case 1:
                                RM692CX_G25.Value = RGBValue[index, 1];
                                break;
                            case 2:
                                RM692CX_B25.Value = RGBValue[index, 2];
                                break;
                        }

                        break;
                    case 6:
                        switch (rgb)
                        {
                            case 0:
                                RM692CX_R24.Value = RGBValue[index, 0];
                                break;
                            case 1:
                                RM692CX_G24.Value = RGBValue[index, 1];
                                break;
                            case 2:
                                RM692CX_B24.Value = RGBValue[index, 2];
                                break;
                        }

                        break;
                    case 7:
                        switch (rgb)
                        {
                            case 0:
                                RM692CX_R23.Value = RGBValue[index, 0];
                                break;
                            case 1:
                                RM692CX_G23.Value = RGBValue[index, 1];
                                break;
                            case 2:
                                RM692CX_B23.Value = RGBValue[index, 2];
                                break;
                        }

                        break;
                    case 8:
                        switch (rgb)
                        {
                            case 0:
                                RM692CX_R22.Value = RGBValue[index, 0];
                                break;
                            case 1:
                                RM692CX_G22.Value = RGBValue[index, 1];
                                break;
                            case 2:
                                RM692CX_B22.Value = RGBValue[index, 2];
                                break;
                        }

                        break;
                    case 9:
                        switch (rgb)
                        {
                            case 0:
                                RM692CX_R21.Value = RGBValue[index, 0];
                                break;
                            case 1:
                                RM692CX_G21.Value = RGBValue[index, 1];
                                break;
                            case 2:
                                RM692CX_B21.Value = RGBValue[index, 2];
                                break;
                        }

                        break;
                    case 10:
                        switch (rgb)
                        {
                            case 0:
                                RM692CX_R20.Value = RGBValue[index, 0];
                                break;
                            case 1:
                                RM692CX_G20.Value = RGBValue[index, 1];
                                break;
                            case 2:
                                RM692CX_B20.Value = RGBValue[index, 2];
                                break;
                        }

                        break;
                    case 11:
                        switch (rgb)
                        {
                            case 0:
                                RM692CX_R19.Value = RGBValue[index, 0];
                                break;
                            case 1:
                                RM692CX_G19.Value = RGBValue[index, 1];
                                break;
                            case 2:
                                RM692CX_B19.Value = RGBValue[index, 2];
                                break;
                        }

                        break;
                    case 12:
                        switch (rgb)
                        {
                            case 0:
                                RM692CX_R18.Value = RGBValue[index, 0];
                                break;
                            case 1:
                                RM692CX_G18.Value = RGBValue[index, 1];
                                break;
                            case 2:
                                RM692CX_B18.Value = RGBValue[index, 2];
                                break;
                        }

                        break;
                    case 13:
                        switch (rgb)
                        {
                            case 0:
                                RM692CX_R17.Value = RGBValue[index, 0];
                                break;
                            case 1:
                                RM692CX_G17.Value = RGBValue[index, 1];
                                break;
                            case 2:
                                RM692CX_B17.Value = RGBValue[index, 2];
                                break;
                        }

                        break;
                    case 14:
                        switch (rgb)
                        {
                            case 0:
                                RM692CX_R16.Value = RGBValue[index, 0];
                                break;
                            case 1:
                                RM692CX_G16.Value = RGBValue[index, 1];
                                break;
                            case 2:
                                RM692CX_B16.Value = RGBValue[index, 2];
                                break;
                        }

                        break;
                    case 15:
                        switch (rgb)
                        {
                            case 0:
                                RM692CX_R15.Value = RGBValue[index, 0];
                                break;
                            case 1:
                                RM692CX_G15.Value = RGBValue[index, 1];
                                break;
                            case 2:
                                RM692CX_B15.Value = RGBValue[index, 2];
                                break;
                        }

                        break;
                    case 16:
                        switch (rgb)
                        {
                            case 0:
                                RM692CX_R14.Value = RGBValue[index, 0];
                                break;
                            case 1:
                                RM692CX_G14.Value = RGBValue[index, 1];
                                break;
                            case 2:
                                RM692CX_B14.Value = RGBValue[index, 2];
                                break;
                        }

                        break;
                    case 17:
                        switch (rgb)
                        {
                            case 0:
                                RM692CX_R13.Value = RGBValue[index, 0];
                                break;
                            case 1:
                                RM692CX_G13.Value = RGBValue[index, 1];
                                break;
                            case 2:
                                RM692CX_B13.Value = RGBValue[index, 2];
                                break;
                        }

                        break;
                    case 18:
                        switch (rgb)
                        {
                            case 0:
                                RM692CX_R12.Value = RGBValue[index, 0];
                                break;
                            case 1:
                                RM692CX_G12.Value = RGBValue[index, 1];
                                break;
                            case 2:
                                RM692CX_B12.Value = RGBValue[index, 2];
                                break;
                        }

                        break;
                    case 19:
                        switch (rgb)
                        {
                            case 0:
                                RM692CX_R11.Value = RGBValue[index, 0];
                                break;
                            case 1:
                                RM692CX_G11.Value = RGBValue[index, 1];
                                break;
                            case 2:
                                RM692CX_B11.Value = RGBValue[index, 2];
                                break;
                        }

                        break;
                    case 20:
                        switch (rgb)
                        {
                            case 0:
                                RM692CX_R10.Value = RGBValue[index, 0];
                                break;
                            case 1:
                                RM692CX_G10.Value = RGBValue[index, 1];
                                break;
                            case 2:
                                RM692CX_B10.Value = RGBValue[index, 2];
                                break;
                        }

                        break;
                    case 21:
                        switch (rgb)
                        {
                            case 0:
                                RM692CX_R9.Value = RGBValue[index, 0];
                                break;
                            case 1:
                                RM692CX_G9.Value = RGBValue[index, 1];
                                break;
                            case 2:
                                RM692CX_B9.Value = RGBValue[index, 2];
                                break;
                        }

                        break;
                    case 22:
                        switch (rgb)
                        {
                            case 0:
                                RM692CX_R8.Value = RGBValue[index, 0];
                                break;
                            case 1:
                                RM692CX_G8.Value = RGBValue[index, 1];
                                break;
                            case 2:
                                RM692CX_B8.Value = RGBValue[index, 2];
                                break;
                        }

                        break;
                    case 23:
                        switch (rgb)
                        {
                            case 0:
                                RM692CX_R7.Value = RGBValue[index, 0];
                                break;
                            case 1:
                                RM692CX_G7.Value = RGBValue[index, 1];
                                break;
                            case 2:
                                RM692CX_B7.Value = RGBValue[index, 2];
                                break;
                        }

                        break;
                    case 24:
                        switch (rgb)
                        {
                            case 0:
                                RM692CX_R6.Value = RGBValue[index, 0];
                                break;
                            case 1:
                                RM692CX_G6.Value = RGBValue[index, 1];
                                break;
                            case 2:
                                RM692CX_B6.Value = RGBValue[index, 2];
                                break;
                        }

                        break;
                    case 25:
                        switch (rgb)
                        {
                            case 0:
                                RM692CX_R5.Value = RGBValue[index, 0];
                                break;
                            case 1:
                                RM692CX_G5.Value = RGBValue[index, 1];
                                break;
                            case 2:
                                RM692CX_B5.Value = RGBValue[index, 2];
                                break;
                        }

                        break;
                    case 26:
                        switch (rgb)
                        {
                            case 0:
                                RM692CX_R4.Value = RGBValue[index, 0];
                                break;
                            case 1:
                                RM692CX_G4.Value = RGBValue[index, 1];
                                break;
                            case 2:
                                RM692CX_B4.Value = RGBValue[index, 2];
                                break;
                        }

                        break;
                    case 27:
                        switch (rgb)
                        {
                            case 0:
                                RM692CX_R3.Value = RGBValue[index, 0];
                                break;
                            case 1:
                                RM692CX_G3.Value = RGBValue[index, 1];
                                break;
                            case 2:
                                RM692CX_B3.Value = RGBValue[index, 2];
                                break;
                        }

                        break;
                    case 28:
                        switch (rgb)
                        {
                            case 0:
                                RM692CX_R2.Value = RGBValue[index, 0];
                                break;
                            case 1:
                                RM692CX_G2.Value = RGBValue[index, 1];
                                break;
                            case 2:
                                RM692CX_B2.Value = RGBValue[index, 2];
                                break;
                        }

                        break;
                    case 29:
                        switch (rgb)
                        {
                            case 0:
                                RM692CX_R1.Value = RGBValue[index, 0];
                                break;
                            case 1:
                                RM692CX_G1.Value = RGBValue[index, 1];
                                break;
                            case 2:
                                RM692CX_B1.Value = RGBValue[index, 2];
                                break;
                        }

                        break;

                    default: break;
                }
            }
        }
        private void RM692CX_Gamma_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;
            Send_buf = new byte[74];
            Setting.TempRecord.cmdLen = 74;
            Send_buf[0] = 0x3F; //RM692CX
            Send_buf[2] = 69;
            Send_buf[3] = 0;

            switch (btnTmp.Name)
            {
                case "RM692CX_GammaR_Red":
                    {
                        Setting.TempRecord.RM692CxCmdType = 0x03;       //      3
                        Send_buf[1] = 0x05;                             //读 Red Gamma
                        Send_buf[3] |= 0x00;
                        if (Page50.Checked == true) { Send_buf[4] = 0x01; }
                        else if (Page52.Checked == true) { Send_buf[4] = 0x02; }
                        else if (Page54.Checked == true) { Send_buf[4] = 0x03; }
                        else if (Page56.Checked == true) { Send_buf[4] = 0x04; }
                        else if (Page58.Checked == true) { Send_buf[4] = 0x05; }
                    }
                    break;
                case "RM692CX_GammaR_Green":
                    {
                        Setting.TempRecord.RM692CxCmdType = 0x04;   //      4
                        Send_buf[1] = 0x05;                         //  读Gamma
                        Send_buf[3] |= 0x42;                        //绿色 green
                        if (Page50.Checked == true) { Send_buf[4] = 0x01; }
                        else if (Page52.Checked == true) { Send_buf[4] = 0x02; }
                        else if (Page54.Checked == true) { Send_buf[4] = 0x03; }
                        else if (Page56.Checked == true) { Send_buf[4] = 0x04; }
                        else if (Page58.Checked == true) { Send_buf[4] = 0x05; }
                    }
                    break;
                case "RM692CX_GammaR_Blue": //              9
                    {
                        Setting.TempRecord.RM692CxCmdType = 0x05;   //      5
                        Send_buf[1] = 0x05;                         //   读Gamma
                        Send_buf[3] |= 0x81;                        //   blue,此处的0x81下位机未使用
                        if (Page50.Checked == true) { Send_buf[4] = 0x01; }
                        else if (Page52.Checked == true) { Send_buf[4] = 0x02; }
                        else if (Page54.Checked == true) { Send_buf[4] = 0x03; }
                        else if (Page56.Checked == true) { Send_buf[4] = 0x04; }
                        else if (Page58.Checked == true) { Send_buf[4] = 0x05; }
                    }
                    break;
                //--------------------------------------
                case "RM692CX_GammaW_Red":
                    {
                        Send_buf[1] = 0x04; //写Gamma
                        if (Page50.Checked == true) { Send_buf[3] = 0x00; }
                        else if (Page52.Checked == true) { Send_buf[3] = 0x01; }
                        else if (Page54.Checked == true) { Send_buf[3] = 0x02; }
                        else if (Page56.Checked == true) { Send_buf[3] = 0x03; }
                        else if (Page58.Checked == true) { Send_buf[3] = 0x04; }
                        //Send_buf[4] = Convert.ToByte(Convert.ToUInt16(RM692CX_R0.Value.ToString()) >> 8);
                        //Send_buf[5] = Convert.ToByte((Convert.ToUInt16(RM692CX_R0.Value.ToString())) & 0xff);
                        Send_buf[6] = Convert.ToByte(Convert.ToUInt16(RM692CX_R1.Value.ToString()) >> 8);
                        Send_buf[7] = Convert.ToByte((Convert.ToUInt16(RM692CX_R1.Value.ToString())) & 0xff);
                        Send_buf[8] = Convert.ToByte(Convert.ToUInt16(RM692CX_R2.Value.ToString()) >> 8);
                        Send_buf[9] = Convert.ToByte((Convert.ToUInt16(RM692CX_R2.Value.ToString())) & 0xff);
                        Send_buf[10] = Convert.ToByte(Convert.ToUInt16(RM692CX_R3.Value.ToString()) >> 8);
                        Send_buf[11] = Convert.ToByte((Convert.ToUInt16(RM692CX_R3.Value.ToString())) & 0xff);
                        Send_buf[12] = Convert.ToByte(Convert.ToUInt16(RM692CX_R4.Value.ToString()) >> 8);
                        Send_buf[13] = Convert.ToByte((Convert.ToUInt16(RM692CX_R4.Value.ToString())) & 0xff);
                        Send_buf[14] = Convert.ToByte(Convert.ToUInt16(RM692CX_R5.Value.ToString()) >> 8);
                        Send_buf[15] = Convert.ToByte((Convert.ToUInt16(RM692CX_R5.Value.ToString())) & 0xff);
                        Send_buf[16] = Convert.ToByte(Convert.ToUInt16(RM692CX_R6.Value.ToString()) >> 8);
                        Send_buf[17] = Convert.ToByte((Convert.ToUInt16(RM692CX_R6.Value.ToString())) & 0xff);
                        Send_buf[18] = Convert.ToByte(Convert.ToUInt16(RM692CX_R7.Value.ToString()) >> 8);
                        Send_buf[19] = Convert.ToByte((Convert.ToUInt16(RM692CX_R7.Value.ToString())) & 0xff);
                        Send_buf[20] = Convert.ToByte(Convert.ToUInt16(RM692CX_R8.Value.ToString()) >> 8);
                        Send_buf[21] = Convert.ToByte((Convert.ToUInt16(RM692CX_R8.Value.ToString())) & 0xff);
                        Send_buf[22] = Convert.ToByte(Convert.ToUInt16(RM692CX_R9.Value.ToString()) >> 8);
                        Send_buf[23] = Convert.ToByte((Convert.ToUInt16(RM692CX_R9.Value.ToString())) & 0xff);
                        Send_buf[24] = Convert.ToByte(Convert.ToUInt16(RM692CX_R10.Value.ToString()) >> 8);
                        Send_buf[25] = Convert.ToByte((Convert.ToUInt16(RM692CX_R10.Value.ToString())) & 0xff);
                        Send_buf[26] = Convert.ToByte(Convert.ToUInt16(RM692CX_R11.Value.ToString()) >> 8);
                        Send_buf[27] = Convert.ToByte((Convert.ToUInt16(RM692CX_R11.Value.ToString())) & 0xff);
                        Send_buf[28] = Convert.ToByte(Convert.ToUInt16(RM692CX_R12.Value.ToString()) >> 8);
                        Send_buf[29] = Convert.ToByte((Convert.ToUInt16(RM692CX_R12.Value.ToString())) & 0xff);
                        Send_buf[30] = Convert.ToByte(Convert.ToUInt16(RM692CX_R13.Value.ToString()) >> 8);
                        Send_buf[31] = Convert.ToByte((Convert.ToUInt16(RM692CX_R13.Value.ToString())) & 0xff);
                        Send_buf[32] = Convert.ToByte(Convert.ToUInt16(RM692CX_R14.Value.ToString()) >> 8);
                        Send_buf[33] = Convert.ToByte((Convert.ToUInt16(RM692CX_R14.Value.ToString())) & 0xff);
                        Send_buf[34] = Convert.ToByte(Convert.ToUInt16(RM692CX_R15.Value.ToString()) >> 8);
                        Send_buf[35] = Convert.ToByte((Convert.ToUInt16(RM692CX_R15.Value.ToString())) & 0xff);
                        Send_buf[36] = Convert.ToByte(Convert.ToUInt16(RM692CX_R16.Value.ToString()) >> 8);
                        Send_buf[37] = Convert.ToByte((Convert.ToUInt16(RM692CX_R16.Value.ToString())) & 0xff);
                        Send_buf[38] = Convert.ToByte(Convert.ToUInt16(RM692CX_R17.Value.ToString()) >> 8);
                        Send_buf[39] = Convert.ToByte((Convert.ToUInt16(RM692CX_R17.Value.ToString())) & 0xff);
                        Send_buf[40] = Convert.ToByte(Convert.ToUInt16(RM692CX_R18.Value.ToString()) >> 8);
                        Send_buf[41] = Convert.ToByte((Convert.ToUInt16(RM692CX_R18.Value.ToString())) & 0xff);
                        Send_buf[42] = Convert.ToByte(Convert.ToUInt16(RM692CX_R19.Value.ToString()) >> 8);
                        Send_buf[43] = Convert.ToByte((Convert.ToUInt16(RM692CX_R19.Value.ToString())) & 0xff);
                        Send_buf[44] = Convert.ToByte(Convert.ToUInt16(RM692CX_R20.Value.ToString()) >> 8);
                        Send_buf[45] = Convert.ToByte((Convert.ToUInt16(RM692CX_R20.Value.ToString())) & 0xff);
                        Send_buf[46] = Convert.ToByte(Convert.ToUInt16(RM692CX_R21.Value.ToString()) >> 8);
                        Send_buf[47] = Convert.ToByte((Convert.ToUInt16(RM692CX_R21.Value.ToString())) & 0xff);
                        Send_buf[48] = Convert.ToByte(Convert.ToUInt16(RM692CX_R22.Value.ToString()) >> 8);
                        Send_buf[49] = Convert.ToByte((Convert.ToUInt16(RM692CX_R22.Value.ToString())) & 0xff);
                        Send_buf[50] = Convert.ToByte(Convert.ToUInt16(RM692CX_R23.Value.ToString()) >> 8);
                        Send_buf[51] = Convert.ToByte((Convert.ToUInt16(RM692CX_R23.Value.ToString())) & 0xff);
                        Send_buf[52] = Convert.ToByte(Convert.ToUInt16(RM692CX_R24.Value.ToString()) >> 8);
                        Send_buf[53] = Convert.ToByte((Convert.ToUInt16(RM692CX_R24.Value.ToString())) & 0xff);
                        Send_buf[54] = Convert.ToByte(Convert.ToUInt16(RM692CX_R25.Value.ToString()) >> 8);
                        Send_buf[55] = Convert.ToByte((Convert.ToUInt16(RM692CX_R25.Value.ToString())) & 0xff);
                        Send_buf[56] = Convert.ToByte(Convert.ToUInt16(RM692CX_R26.Value.ToString()) >> 8);
                        Send_buf[57] = Convert.ToByte((Convert.ToUInt16(RM692CX_R26.Value.ToString())) & 0xff);
                        Send_buf[58] = Convert.ToByte(Convert.ToUInt16(RM692CX_R27.Value.ToString()) >> 8);
                        Send_buf[59] = Convert.ToByte((Convert.ToUInt16(RM692CX_R27.Value.ToString())) & 0xff);
                        Send_buf[60] = Convert.ToByte(Convert.ToUInt16(RM692CX_R28.Value.ToString()) >> 8);
                        Send_buf[61] = Convert.ToByte((Convert.ToUInt16(RM692CX_R28.Value.ToString())) & 0xff);
                        Send_buf[62] = Convert.ToByte(Convert.ToUInt16(RM692CX_R29.Value.ToString()) >> 8);
                        Send_buf[63] = Convert.ToByte((Convert.ToUInt16(RM692CX_R29.Value.ToString())) & 0xff);
                        Send_buf[64] = Convert.ToByte(Convert.ToUInt16(RM692CX_R30.Value.ToString()) >> 8);
                        Send_buf[65] = Convert.ToByte((Convert.ToUInt16(RM692CX_R30.Value.ToString())) & 0xff);
                        //Send_buf[66] = Convert.ToByte(Convert.ToUInt16(RM692CX_R31.Value.ToString()) >> 8);
                        //Send_buf[67] = Convert.ToByte((Convert.ToUInt16(RM692CX_R31.Value.ToString())) & 0xff);
                        //Send_buf[68] = Convert.ToByte(Convert.ToUInt16(RM692CX_R32.Value.ToString()) >> 8);
                        //Send_buf[69] = Convert.ToByte((Convert.ToUInt16(RM692CX_R32.Value.ToString())) & 0xff);
                        //Send_buf[70] = Convert.ToByte(Convert.ToUInt16(RM692CX_RFPS.Value.ToString()) >> 8);
                        //Send_buf[71] = Convert.ToByte((Convert.ToUInt16(RM692CX_RFPS.Value.ToString())) & 0xff);
                    }
                    break;
                case "RM692CX_GammaW_Green":
                    {
                        Send_buf[1] = 0x04;
                        if (Page50.Checked == true) { Send_buf[3] = 0x40; }
                        else if (Page52.Checked == true) { Send_buf[3] = 0x41; }
                        else if (Page54.Checked == true) { Send_buf[3] = 0x42; }
                        else if (Page56.Checked == true) { Send_buf[3] = 0x43; }
                        else if (Page58.Checked == true) { Send_buf[3] = 0x44; }
                        //Send_buf[4] = Convert.ToByte(Convert.ToUInt16(RM692CX_G0.Value.ToString()) >> 8);
                        //Send_buf[5] = Convert.ToByte((Convert.ToUInt16(RM692CX_G0.Value.ToString())) & 0xff);
                        Send_buf[6] = Convert.ToByte(Convert.ToUInt16(RM692CX_G1.Value.ToString()) >> 8);
                        Send_buf[7] = Convert.ToByte((Convert.ToUInt16(RM692CX_G1.Value.ToString())) & 0xff);
                        Send_buf[8] = Convert.ToByte(Convert.ToUInt16(RM692CX_G2.Value.ToString()) >> 8);
                        Send_buf[9] = Convert.ToByte((Convert.ToUInt16(RM692CX_G2.Value.ToString())) & 0xff);
                        Send_buf[10] = Convert.ToByte(Convert.ToUInt16(RM692CX_G3.Value.ToString()) >> 8);
                        Send_buf[11] = Convert.ToByte((Convert.ToUInt16(RM692CX_G3.Value.ToString())) & 0xff);
                        Send_buf[12] = Convert.ToByte(Convert.ToUInt16(RM692CX_G4.Value.ToString()) >> 8);
                        Send_buf[13] = Convert.ToByte((Convert.ToUInt16(RM692CX_G4.Value.ToString())) & 0xff);
                        Send_buf[14] = Convert.ToByte(Convert.ToUInt16(RM692CX_G5.Value.ToString()) >> 8);
                        Send_buf[15] = Convert.ToByte((Convert.ToUInt16(RM692CX_G5.Value.ToString())) & 0xff);
                        Send_buf[16] = Convert.ToByte(Convert.ToUInt16(RM692CX_G6.Value.ToString()) >> 8);
                        Send_buf[17] = Convert.ToByte((Convert.ToUInt16(RM692CX_G6.Value.ToString())) & 0xff);
                        Send_buf[18] = Convert.ToByte(Convert.ToUInt16(RM692CX_G7.Value.ToString()) >> 8);
                        Send_buf[19] = Convert.ToByte((Convert.ToUInt16(RM692CX_G7.Value.ToString())) & 0xff);
                        Send_buf[20] = Convert.ToByte(Convert.ToUInt16(RM692CX_G8.Value.ToString()) >> 8);
                        Send_buf[21] = Convert.ToByte((Convert.ToUInt16(RM692CX_G8.Value.ToString())) & 0xff);
                        Send_buf[22] = Convert.ToByte(Convert.ToUInt16(RM692CX_G9.Value.ToString()) >> 8);
                        Send_buf[23] = Convert.ToByte((Convert.ToUInt16(RM692CX_G9.Value.ToString())) & 0xff);
                        Send_buf[24] = Convert.ToByte(Convert.ToUInt16(RM692CX_G10.Value.ToString()) >> 8);
                        Send_buf[25] = Convert.ToByte((Convert.ToUInt16(RM692CX_G10.Value.ToString())) & 0xff);
                        Send_buf[26] = Convert.ToByte(Convert.ToUInt16(RM692CX_G11.Value.ToString()) >> 8);
                        Send_buf[27] = Convert.ToByte((Convert.ToUInt16(RM692CX_G11.Value.ToString())) & 0xff);
                        Send_buf[28] = Convert.ToByte(Convert.ToUInt16(RM692CX_G12.Value.ToString()) >> 8);
                        Send_buf[29] = Convert.ToByte((Convert.ToUInt16(RM692CX_G12.Value.ToString())) & 0xff);
                        Send_buf[30] = Convert.ToByte(Convert.ToUInt16(RM692CX_G13.Value.ToString()) >> 8);
                        Send_buf[31] = Convert.ToByte((Convert.ToUInt16(RM692CX_G13.Value.ToString())) & 0xff);
                        Send_buf[32] = Convert.ToByte(Convert.ToUInt16(RM692CX_G14.Value.ToString()) >> 8);
                        Send_buf[33] = Convert.ToByte((Convert.ToUInt16(RM692CX_G14.Value.ToString())) & 0xff);
                        Send_buf[34] = Convert.ToByte(Convert.ToUInt16(RM692CX_G15.Value.ToString()) >> 8);
                        Send_buf[35] = Convert.ToByte((Convert.ToUInt16(RM692CX_G15.Value.ToString())) & 0xff);
                        Send_buf[36] = Convert.ToByte(Convert.ToUInt16(RM692CX_G16.Value.ToString()) >> 8);
                        Send_buf[37] = Convert.ToByte((Convert.ToUInt16(RM692CX_G16.Value.ToString())) & 0xff);
                        Send_buf[38] = Convert.ToByte(Convert.ToUInt16(RM692CX_G17.Value.ToString()) >> 8);
                        Send_buf[39] = Convert.ToByte((Convert.ToUInt16(RM692CX_G17.Value.ToString())) & 0xff);
                        Send_buf[40] = Convert.ToByte(Convert.ToUInt16(RM692CX_G18.Value.ToString()) >> 8);
                        Send_buf[41] = Convert.ToByte((Convert.ToUInt16(RM692CX_G18.Value.ToString())) & 0xff);
                        Send_buf[42] = Convert.ToByte(Convert.ToUInt16(RM692CX_G19.Value.ToString()) >> 8);
                        Send_buf[43] = Convert.ToByte((Convert.ToUInt16(RM692CX_G19.Value.ToString())) & 0xff);
                        Send_buf[44] = Convert.ToByte(Convert.ToUInt16(RM692CX_G20.Value.ToString()) >> 8);
                        Send_buf[45] = Convert.ToByte((Convert.ToUInt16(RM692CX_G20.Value.ToString())) & 0xff);
                        Send_buf[46] = Convert.ToByte(Convert.ToUInt16(RM692CX_G21.Value.ToString()) >> 8);
                        Send_buf[47] = Convert.ToByte((Convert.ToUInt16(RM692CX_G21.Value.ToString())) & 0xff);
                        Send_buf[48] = Convert.ToByte(Convert.ToUInt16(RM692CX_G22.Value.ToString()) >> 8);
                        Send_buf[49] = Convert.ToByte((Convert.ToUInt16(RM692CX_G22.Value.ToString())) & 0xff);
                        Send_buf[50] = Convert.ToByte(Convert.ToUInt16(RM692CX_G23.Value.ToString()) >> 8);
                        Send_buf[51] = Convert.ToByte((Convert.ToUInt16(RM692CX_G23.Value.ToString())) & 0xff);
                        Send_buf[52] = Convert.ToByte(Convert.ToUInt16(RM692CX_G24.Value.ToString()) >> 8);
                        Send_buf[53] = Convert.ToByte((Convert.ToUInt16(RM692CX_G24.Value.ToString())) & 0xff);
                        Send_buf[54] = Convert.ToByte(Convert.ToUInt16(RM692CX_G25.Value.ToString()) >> 8);
                        Send_buf[55] = Convert.ToByte((Convert.ToUInt16(RM692CX_G25.Value.ToString())) & 0xff);
                        Send_buf[56] = Convert.ToByte(Convert.ToUInt16(RM692CX_G26.Value.ToString()) >> 8);
                        Send_buf[57] = Convert.ToByte((Convert.ToUInt16(RM692CX_G26.Value.ToString())) & 0xff);
                        Send_buf[58] = Convert.ToByte(Convert.ToUInt16(RM692CX_G27.Value.ToString()) >> 8);
                        Send_buf[59] = Convert.ToByte((Convert.ToUInt16(RM692CX_G27.Value.ToString())) & 0xff);
                        Send_buf[60] = Convert.ToByte(Convert.ToUInt16(RM692CX_G28.Value.ToString()) >> 8);
                        Send_buf[61] = Convert.ToByte((Convert.ToUInt16(RM692CX_G28.Value.ToString())) & 0xff);
                        Send_buf[62] = Convert.ToByte(Convert.ToUInt16(RM692CX_G29.Value.ToString()) >> 8);
                        Send_buf[63] = Convert.ToByte((Convert.ToUInt16(RM692CX_G29.Value.ToString())) & 0xff);
                        Send_buf[64] = Convert.ToByte(Convert.ToUInt16(RM692CX_G30.Value.ToString()) >> 8);
                        Send_buf[65] = Convert.ToByte((Convert.ToUInt16(RM692CX_G30.Value.ToString())) & 0xff);
                        //Send_buf[66] = Convert.ToByte(Convert.ToUInt16(RM692CX_G31.Value.ToString()) >> 8);
                        //Send_buf[67] = Convert.ToByte((Convert.ToUInt16(RM692CX_G31.Value.ToString())) & 0xff);
                        //Send_buf[68] = Convert.ToByte(Convert.ToUInt16(RM692CX_G32.Value.ToString()) >> 8);
                        //Send_buf[69] = Convert.ToByte((Convert.ToUInt16(RM692CX_G32.Value.ToString())) & 0xff);
                        //Send_buf[70] = Convert.ToByte(Convert.ToUInt16(RM692CX_GFPS.Value.ToString()) >> 8);
                        //Send_buf[71] = Convert.ToByte((Convert.ToUInt16(RM692CX_GFPS.Value.ToString())) & 0xff);
                    }
                    break;
                case "RM692CX_GammaW_Blue":     //      8
                    {
                        Send_buf[1] = 0x04;
                        if (Page50.Checked == true) { Send_buf[3] = 0x80; }
                        else if (Page52.Checked == true) { Send_buf[3] = 0x81; }
                        else if (Page54.Checked == true) { Send_buf[3] = 0x82; }
                        else if (Page56.Checked == true) { Send_buf[3] = 0x83; }
                        else if (Page58.Checked == true) { Send_buf[3] = 0x84; }
                        //Send_buf[4] = Convert.ToByte(Convert.ToUInt16(RM692CX_B0.Value.ToString()) >> 8);
                        //Send_buf[5] = Convert.ToByte((Convert.ToUInt16(RM692CX_B0.Value.ToString())) & 0xff);
                        Send_buf[6] = Convert.ToByte(Convert.ToUInt16(RM692CX_B1.Value.ToString()) >> 8);
                        Send_buf[7] = Convert.ToByte((Convert.ToUInt16(RM692CX_B1.Value.ToString())) & 0xff);
                        Send_buf[8] = Convert.ToByte(Convert.ToUInt16(RM692CX_B2.Value.ToString()) >> 8);
                        Send_buf[9] = Convert.ToByte((Convert.ToUInt16(RM692CX_B2.Value.ToString())) & 0xff);
                        Send_buf[10] = Convert.ToByte(Convert.ToUInt16(RM692CX_B3.Value.ToString()) >> 8);
                        Send_buf[11] = Convert.ToByte((Convert.ToUInt16(RM692CX_B3.Value.ToString())) & 0xff);
                        Send_buf[12] = Convert.ToByte(Convert.ToUInt16(RM692CX_B4.Value.ToString()) >> 8);
                        Send_buf[13] = Convert.ToByte((Convert.ToUInt16(RM692CX_B4.Value.ToString())) & 0xff);
                        Send_buf[14] = Convert.ToByte(Convert.ToUInt16(RM692CX_B5.Value.ToString()) >> 8);
                        Send_buf[15] = Convert.ToByte((Convert.ToUInt16(RM692CX_B5.Value.ToString())) & 0xff);
                        Send_buf[16] = Convert.ToByte(Convert.ToUInt16(RM692CX_B6.Value.ToString()) >> 8);
                        Send_buf[17] = Convert.ToByte((Convert.ToUInt16(RM692CX_B6.Value.ToString())) & 0xff);
                        Send_buf[18] = Convert.ToByte(Convert.ToUInt16(RM692CX_B7.Value.ToString()) >> 8);
                        Send_buf[19] = Convert.ToByte((Convert.ToUInt16(RM692CX_B7.Value.ToString())) & 0xff);
                        Send_buf[20] = Convert.ToByte(Convert.ToUInt16(RM692CX_B8.Value.ToString()) >> 8);
                        Send_buf[21] = Convert.ToByte((Convert.ToUInt16(RM692CX_B8.Value.ToString())) & 0xff);
                        Send_buf[22] = Convert.ToByte(Convert.ToUInt16(RM692CX_B9.Value.ToString()) >> 8);
                        Send_buf[23] = Convert.ToByte((Convert.ToUInt16(RM692CX_B9.Value.ToString())) & 0xff);
                        Send_buf[24] = Convert.ToByte(Convert.ToUInt16(RM692CX_B10.Value.ToString()) >> 8);
                        Send_buf[25] = Convert.ToByte((Convert.ToUInt16(RM692CX_B10.Value.ToString())) & 0xff);
                        Send_buf[26] = Convert.ToByte(Convert.ToUInt16(RM692CX_B11.Value.ToString()) >> 8);
                        Send_buf[27] = Convert.ToByte((Convert.ToUInt16(RM692CX_B11.Value.ToString())) & 0xff);
                        Send_buf[28] = Convert.ToByte(Convert.ToUInt16(RM692CX_B12.Value.ToString()) >> 8);
                        Send_buf[29] = Convert.ToByte((Convert.ToUInt16(RM692CX_B12.Value.ToString())) & 0xff);
                        Send_buf[30] = Convert.ToByte(Convert.ToUInt16(RM692CX_B13.Value.ToString()) >> 8);
                        Send_buf[31] = Convert.ToByte((Convert.ToUInt16(RM692CX_B13.Value.ToString())) & 0xff);
                        Send_buf[32] = Convert.ToByte(Convert.ToUInt16(RM692CX_B14.Value.ToString()) >> 8);
                        Send_buf[33] = Convert.ToByte((Convert.ToUInt16(RM692CX_B14.Value.ToString())) & 0xff);
                        Send_buf[34] = Convert.ToByte(Convert.ToUInt16(RM692CX_B15.Value.ToString()) >> 8);
                        Send_buf[35] = Convert.ToByte((Convert.ToUInt16(RM692CX_B15.Value.ToString())) & 0xff);
                        Send_buf[36] = Convert.ToByte(Convert.ToUInt16(RM692CX_B16.Value.ToString()) >> 8);
                        Send_buf[37] = Convert.ToByte((Convert.ToUInt16(RM692CX_B16.Value.ToString())) & 0xff);
                        Send_buf[38] = Convert.ToByte(Convert.ToUInt16(RM692CX_B17.Value.ToString()) >> 8);
                        Send_buf[39] = Convert.ToByte((Convert.ToUInt16(RM692CX_B17.Value.ToString())) & 0xff);
                        Send_buf[40] = Convert.ToByte(Convert.ToUInt16(RM692CX_B18.Value.ToString()) >> 8);
                        Send_buf[41] = Convert.ToByte((Convert.ToUInt16(RM692CX_B18.Value.ToString())) & 0xff);
                        Send_buf[42] = Convert.ToByte(Convert.ToUInt16(RM692CX_B19.Value.ToString()) >> 8);
                        Send_buf[43] = Convert.ToByte((Convert.ToUInt16(RM692CX_B19.Value.ToString())) & 0xff);
                        Send_buf[44] = Convert.ToByte(Convert.ToUInt16(RM692CX_B20.Value.ToString()) >> 8);
                        Send_buf[45] = Convert.ToByte((Convert.ToUInt16(RM692CX_B20.Value.ToString())) & 0xff);
                        Send_buf[46] = Convert.ToByte(Convert.ToUInt16(RM692CX_B21.Value.ToString()) >> 8);
                        Send_buf[47] = Convert.ToByte((Convert.ToUInt16(RM692CX_B21.Value.ToString())) & 0xff);
                        Send_buf[48] = Convert.ToByte(Convert.ToUInt16(RM692CX_B22.Value.ToString()) >> 8);
                        Send_buf[49] = Convert.ToByte((Convert.ToUInt16(RM692CX_B22.Value.ToString())) & 0xff);
                        Send_buf[50] = Convert.ToByte(Convert.ToUInt16(RM692CX_B23.Value.ToString()) >> 8);
                        Send_buf[51] = Convert.ToByte((Convert.ToUInt16(RM692CX_B23.Value.ToString())) & 0xff);
                        Send_buf[52] = Convert.ToByte(Convert.ToUInt16(RM692CX_B24.Value.ToString()) >> 8);
                        Send_buf[53] = Convert.ToByte((Convert.ToUInt16(RM692CX_B24.Value.ToString())) & 0xff);
                        Send_buf[54] = Convert.ToByte(Convert.ToUInt16(RM692CX_B25.Value.ToString()) >> 8);
                        Send_buf[55] = Convert.ToByte((Convert.ToUInt16(RM692CX_B25.Value.ToString())) & 0xff);
                        Send_buf[56] = Convert.ToByte(Convert.ToUInt16(RM692CX_B26.Value.ToString()) >> 8);
                        Send_buf[57] = Convert.ToByte((Convert.ToUInt16(RM692CX_B26.Value.ToString())) & 0xff);
                        Send_buf[58] = Convert.ToByte(Convert.ToUInt16(RM692CX_B27.Value.ToString()) >> 8);
                        Send_buf[59] = Convert.ToByte((Convert.ToUInt16(RM692CX_B27.Value.ToString())) & 0xff);
                        Send_buf[60] = Convert.ToByte(Convert.ToUInt16(RM692CX_B28.Value.ToString()) >> 8);
                        Send_buf[61] = Convert.ToByte((Convert.ToUInt16(RM692CX_B28.Value.ToString())) & 0xff);
                        Send_buf[62] = Convert.ToByte(Convert.ToUInt16(RM692CX_B29.Value.ToString()) >> 8);
                        Send_buf[63] = Convert.ToByte((Convert.ToUInt16(RM692CX_B29.Value.ToString())) & 0xff);
                        Send_buf[64] = Convert.ToByte(Convert.ToUInt16(RM692CX_B30.Value.ToString()) >> 8);
                        Send_buf[65] = Convert.ToByte((Convert.ToUInt16(RM692CX_B30.Value.ToString())) & 0xff);
                        //Send_buf[66] = Convert.ToByte(Convert.ToUInt16(RM692CX_B31.Value.ToString()) >> 8);
                        //Send_buf[67] = Convert.ToByte((Convert.ToUInt16(RM692CX_B31.Value.ToString())) & 0xff);
                        //Send_buf[68] = Convert.ToByte(Convert.ToUInt16(RM692CX_B32.Value.ToString()) >> 8);
                        //Send_buf[69] = Convert.ToByte((Convert.ToUInt16(RM692CX_B32.Value.ToString())) & 0xff);
                        //Send_buf[70] = Convert.ToByte(Convert.ToUInt16(RM692CX_BFPS.Value.ToString()) >> 8);
                        //Send_buf[71] = Convert.ToByte((Convert.ToUInt16(RM692CX_BFPS.Value.ToString())) & 0xff);

                    }
                    break;
                default:
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[72], out Send_buf[73]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 74);   //RM69350
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            T1.Enabled = false;
            T1.Interval = 2000;
            T1.Enabled = true;
        }

        private int[] Point_692AX = new int[30] {//---Gamma绑点数据，这里的数据仅供参考---
            255, 254, 252, 248, 244, 236, 228, 220, 212, 204, 196, 188, 180, 164, 148, 132, 116, 100, 84, 68, 52, 44, 36, 28, 20, 16, 12, 8, 4, 3 };

        private void btnCalculate_RM692CX(object sender, EventArgs e)//--设定绑点数据
        {
            NumericUpDown[] NUMP = new NumericUpDown[] { RM692CX_30, RM692CX_29, RM692CX_28, RM692CX_27, RM692CX_26, RM692CX_25, RM692CX_24, RM692CX_23, RM692CX_22, RM692CX_21, RM692CX_20, RM692CX_19, RM692CX_18, RM692CX_17, RM692CX_16, RM692CX_15, RM692CX_14, RM692CX_13, RM692CX_12, RM692CX_11, RM692CX_10, RM692CX_9, RM692CX_8, RM692CX_7, RM692CX_6, RM692CX_5, RM692CX_4, RM692CX_3, RM692CX_2, RM692CX_1 };
            txt2AX_lum.Text = txtTargetLum.Text;
            for (int i = 0; i < 30; i++)
            {
                Point_692AX[i] = Convert.ToByte(NUMP[i].Value); //255
            }
            //Point_692AX[0] = Convert.ToByte(NUMP[i].Value); //255

            RM692CX_P30.Text = (Math.Pow((Point_692AX[0] / 255.0), Convert.ToDouble(txt2AX_Gamma.Text)) * Convert.ToDouble(txt2AX_lum.Text)).ToString("F4");
            RM692CX_P29.Text = (Math.Pow((Point_692AX[1] / 255.0), Convert.ToDouble(txt2AX_Gamma.Text)) * Convert.ToDouble(txt2AX_lum.Text)).ToString("F4");
            RM692CX_P28.Text = (Math.Pow((Point_692AX[2] / 255.0), Convert.ToDouble(txt2AX_Gamma.Text)) * Convert.ToDouble(txt2AX_lum.Text)).ToString("F4");
            RM692CX_P27.Text = (Math.Pow((Point_692AX[3] / 255.0), Convert.ToDouble(txt2AX_Gamma.Text)) * Convert.ToDouble(txt2AX_lum.Text)).ToString("F4");
            RM692CX_P26.Text = (Math.Pow((Point_692AX[4] / 255.0), Convert.ToDouble(txt2AX_Gamma.Text)) * Convert.ToDouble(txt2AX_lum.Text)).ToString("F4");
            RM692CX_P25.Text = (Math.Pow((Point_692AX[5] / 255.0), Convert.ToDouble(txt2AX_Gamma.Text)) * Convert.ToDouble(txt2AX_lum.Text)).ToString("F4");
            RM692CX_P24.Text = (Math.Pow((Point_692AX[6] / 255.0), Convert.ToDouble(txt2AX_Gamma.Text)) * Convert.ToDouble(txt2AX_lum.Text)).ToString("F4");
            RM692CX_P23.Text = (Math.Pow((Point_692AX[7] / 255.0), Convert.ToDouble(txt2AX_Gamma.Text)) * Convert.ToDouble(txt2AX_lum.Text)).ToString("F4");
            RM692CX_P22.Text = (Math.Pow((Point_692AX[8] / 255.0), Convert.ToDouble(txt2AX_Gamma.Text)) * Convert.ToDouble(txt2AX_lum.Text)).ToString("F4");
            RM692CX_P21.Text = (Math.Pow((Point_692AX[9] / 255.0), Convert.ToDouble(txt2AX_Gamma.Text)) * Convert.ToDouble(txt2AX_lum.Text)).ToString("F4");
            RM692CX_P20.Text = (Math.Pow((Point_692AX[10] / 255.0), Convert.ToDouble(txt2AX_Gamma.Text)) * Convert.ToDouble(txt2AX_lum.Text)).ToString("F4");
            RM692CX_P19.Text = (Math.Pow((Point_692AX[11] / 255.0), Convert.ToDouble(txt2AX_Gamma.Text)) * Convert.ToDouble(txt2AX_lum.Text)).ToString("F4");
            RM692CX_P18.Text = (Math.Pow((Point_692AX[12] / 255.0), Convert.ToDouble(txt2AX_Gamma.Text)) * Convert.ToDouble(txt2AX_lum.Text)).ToString("F4");
            RM692CX_P17.Text = (Math.Pow((Point_692AX[13] / 255.0), Convert.ToDouble(txt2AX_Gamma.Text)) * Convert.ToDouble(txt2AX_lum.Text)).ToString("F4");
            RM692CX_P16.Text = (Math.Pow((Point_692AX[14] / 255.0), Convert.ToDouble(txt2AX_Gamma.Text)) * Convert.ToDouble(txt2AX_lum.Text)).ToString("F4");
            RM692CX_P15.Text = (Math.Pow((Point_692AX[15] / 255.0), Convert.ToDouble(txt2AX_Gamma.Text)) * Convert.ToDouble(txt2AX_lum.Text)).ToString("F4");
            RM692CX_P14.Text = (Math.Pow((Point_692AX[16] / 255.0), Convert.ToDouble(txt2AX_Gamma.Text)) * Convert.ToDouble(txt2AX_lum.Text)).ToString("F4");
            RM692CX_P13.Text = (Math.Pow((Point_692AX[17] / 255.0), Convert.ToDouble(txt2AX_Gamma.Text)) * Convert.ToDouble(txt2AX_lum.Text)).ToString("F4");
            RM692CX_P12.Text = (Math.Pow((Point_692AX[18] / 255.0), Convert.ToDouble(txt2AX_Gamma.Text)) * Convert.ToDouble(txt2AX_lum.Text)).ToString("F4");
            RM692CX_P11.Text = (Math.Pow((Point_692AX[19] / 255.0), Convert.ToDouble(txt2AX_Gamma.Text)) * Convert.ToDouble(txt2AX_lum.Text)).ToString("F4");
            RM692CX_P10.Text = (Math.Pow((Point_692AX[20] / 255.0), Convert.ToDouble(txt2AX_Gamma.Text)) * Convert.ToDouble(txt2AX_lum.Text)).ToString("F4");
            RM692CX_P9.Text = (Math.Pow((Point_692AX[21] / 255.0), Convert.ToDouble(txt2AX_Gamma.Text)) * Convert.ToDouble(txt2AX_lum.Text)).ToString("F4");
            RM692CX_P8.Text = (Math.Pow((Point_692AX[22] / 255.0), Convert.ToDouble(txt2AX_Gamma.Text)) * Convert.ToDouble(txt2AX_lum.Text)).ToString("F4");
            RM692CX_P7.Text = (Math.Pow((Point_692AX[23] / 255.0), Convert.ToDouble(txt2AX_Gamma.Text)) * Convert.ToDouble(txt2AX_lum.Text)).ToString("F4");
            RM692CX_P6.Text = (Math.Pow((Point_692AX[24] / 255.0), Convert.ToDouble(txt2AX_Gamma.Text)) * Convert.ToDouble(txt2AX_lum.Text)).ToString("F4");
            RM692CX_P5.Text = (Math.Pow((Point_692AX[25] / 255.0), Convert.ToDouble(txt2AX_Gamma.Text)) * Convert.ToDouble(txt2AX_lum.Text)).ToString("F4");
            RM692CX_P4.Text = (Math.Pow((Point_692AX[26] / 255.0), Convert.ToDouble(txt2AX_Gamma.Text)) * Convert.ToDouble(txt2AX_lum.Text)).ToString("F4");
            RM692CX_P3.Text = (Math.Pow((Point_692AX[27] / 255.0), Convert.ToDouble(txt2AX_Gamma.Text)) * Convert.ToDouble(txt2AX_lum.Text)).ToString("F4");
            RM692CX_P2.Text = (Math.Pow((Point_692AX[28] / 255.0), Convert.ToDouble(txt2AX_Gamma.Text)) * Convert.ToDouble(txt2AX_lum.Text)).ToString("F4");
            RM692CX_P1.Text = (Math.Pow((Point_692AX[29] / 255.0), Convert.ToDouble(txt2AX_Gamma.Text)) * Convert.ToDouble(txt2AX_lum.Text)).ToString("F4");
            //RM692CX_P0.Text = (Math.Pow((Point_692AX[32] / 255.0), Convert.ToDouble(txt2AX_Gamma.Text)) * Convert.ToDouble(txt2AX_lum.Text)).ToString("F4");
        }
        #region  //Gamma调整计时
        System.DateTime TimeNow = new DateTime();
        TimeSpan TimeCount = new TimeSpan();
        private void TimTest_Tick(object sender, EventArgs e)
        {
            TimeCount = DateTime.Now - TimeNow;
            txtTimeShow.Text = string.Format("{0:00}:{1:00}:{2:00}", TimeCount.Hours, TimeCount.Minutes, TimeCount.Seconds);
        }
        #endregion
        private void btnAutogamma2AX_Click(object sender, EventArgs e)
        {
            //BtnSetRGBBase_Click(btnSetRGBBase,e);
            //btnCalculate_RM692CX(btnCalculateRM692CX, e);   //计算绑点目标值
            //Thread.Sleep(17);
            TimTest.Start();
            TimeNow = DateTime.Now; //开始计时
            btnVerfyGamma.Enabled = true;

            teppoint = 1; //记录绑点是否变更        
            frgb = 1;//当前调节RGB标志， 1:r;2:G;3:B;
            fdirr = true;
            fdirg = true;
            fdirb = true;
            teppoint = 1; //记录绑点是否变更        
            frgb = 1;//当前调节RGB标志， 1:r;2:G;3:B;

            int flag = 1;
            double Lr_d, Lg_d, Lb_d;
            D_lum = new double[30]; //每阶目标亮度值
            D_lums = new double[30, 3]; //填入亮度拆分后的RGB
            index = 0;
            adjust_Rnum = 0;
            adjust_Gnum = 0;
            adjust_Bnum = 0;
            adjust_l = 0;
            adjust_x = 0;
            adjust_y = 0;
            flag1 = flag2 = flag3 = 1; GammaAdjustTimes = 0;
            k = 0;
            Button btnTmp = (Button)sender;

            #region//--收集目标亮度值---

            D_lum[0] = Convert.ToDouble(RM692CX_P30.Text);
            D_lum[1] = Convert.ToDouble(RM692CX_P29.Text);
            D_lum[2] = Convert.ToDouble(RM692CX_P28.Text);
            D_lum[3] = Convert.ToDouble(RM692CX_P27.Text);
            D_lum[4] = Convert.ToDouble(RM692CX_P26.Text);
            D_lum[5] = Convert.ToDouble(RM692CX_P25.Text);
            D_lum[6] = Convert.ToDouble(RM692CX_P24.Text);
            D_lum[7] = Convert.ToDouble(RM692CX_P23.Text);
            D_lum[8] = Convert.ToDouble(RM692CX_P22.Text);
            D_lum[9] = Convert.ToDouble(RM692CX_P21.Text);
            D_lum[10] = Convert.ToDouble(RM692CX_P20.Text);
            D_lum[11] = Convert.ToDouble(RM692CX_P19.Text);
            D_lum[12] = Convert.ToDouble(RM692CX_P18.Text);
            D_lum[13] = Convert.ToDouble(RM692CX_P17.Text);
            D_lum[14] = Convert.ToDouble(RM692CX_P16.Text);
            D_lum[15] = Convert.ToDouble(RM692CX_P15.Text);
            D_lum[16] = Convert.ToDouble(RM692CX_P14.Text);
            D_lum[17] = Convert.ToDouble(RM692CX_P13.Text);
            D_lum[18] = Convert.ToDouble(RM692CX_P12.Text);
            D_lum[19] = Convert.ToDouble(RM692CX_P11.Text);
            D_lum[20] = Convert.ToDouble(RM692CX_P10.Text);
            D_lum[21] = Convert.ToDouble(RM692CX_P9.Text);
            D_lum[22] = Convert.ToDouble(RM692CX_P8.Text);
            D_lum[23] = Convert.ToDouble(RM692CX_P7.Text);
            D_lum[24] = Convert.ToDouble(RM692CX_P6.Text);
            D_lum[25] = Convert.ToDouble(RM692CX_P5.Text);
            D_lum[26] = Convert.ToDouble(RM692CX_P4.Text);
            D_lum[27] = Convert.ToDouble(RM692CX_P3.Text);
            D_lum[28] = Convert.ToDouble(RM692CX_P2.Text);
            D_lum[29] = Convert.ToDouble(RM692CX_P1.Text);

            #endregion


            D_x = Convert.ToDouble(txt2AX_X.Text);
            D_y = Convert.ToDouble(txt2AX_Y.Text);
            Lr_d = R_y * ((D_y - G_y) * (B_x - D_x) + (D_x - G_x) * (D_y - B_y)) / B_y / ((D_y - G_y) * (D_x - R_x) + (D_x - G_x) * (R_y - D_y));
            Lb_d = 1.0;
            Lg_d = G_y * ((D_y - R_y) * (B_x - D_x) + (D_x - R_x) * (D_y - B_y)) / B_y / ((D_y - R_y) * (D_x - G_x) + (D_x - R_x) * (G_y - D_y));

            for (int i = 0; i < 30; i++)    //保存目标绑点RGB分亮度
            {
                D_lums[i, 0] = D_lum[i] * Lr_d / (Lr_d + Lg_d + Lb_d);
                D_lums[i, 1] = D_lum[i] * Lg_d / (Lr_d + Lg_d + Lb_d);
                D_lums[i, 2] = D_lum[i] * Lb_d / (Lr_d + Lg_d + Lb_d);
            }
            gamma_r = GAMMAs.RM692CX_Gamma_r; gamma_g = GAMMAs.RM692CX_Gamma_g; gamma_b = GAMMAs.RM692CX_Gamma_b;   //指定绑点寄存器地址
            //显示Gamma调节初始画面，并打开定时器开始调整Gamma
            switch (flag)
            {
                case 1://寄存器值与亮度正比
                    {
                        lum_off = Convert.ToDouble(txt2AX_lumoff.Text);
                        x_off = Convert.ToDouble(txt2AX_Xoff.Text);
                        y_off = Convert.ToDouble(txt2AX_Yoff.Text);
                        if (Vesa_Check.Checked == true)
                        {
                            RM692Cx_btnPrevPic_Run("A" + Point_692AX[0].ToString() + ".dsc_0001.bmp");
                        }
                        else if ((!chkGIRGamma.Checked))
                        {
                            OLED_Show_Click(btnwhite, e);
                        };

                        Msr_CA310();
                        T18.Enabled = true;
                    }
                    break;
                case 2://寄存器值与亮度反比
                    break;
                default:
                    break;
            }
        }

        private static int GammaAdjustTimes = 0;
        //--------------------------------------------------------------------
        private int teppoint = 1; //记录绑点是否变更        
        private int frgb = 1;//当前调节RGB标志， 1:r;2:G;3:B;
        //private int numgammastep=numGammstep.Value;
        int step_tmpr = 64, step_tmpg = 64, step_tmpb = 64;//调节初始步长
        bool fdirr = true;
        bool fdirg = true;
        bool fdirb = true;
        byte nowdirection = 0, lastdirection = 0;
        byte nowdirectionG = 0, lastdirectionG = 0; byte nowdirectionB = 0, lastdirectionB = 0;
        byte GammaAdjN_COUNT = 0; byte GammaAdjN_COUNTG = 0; byte GammaAdjN_COUNTB = 0;
        private void GammaUpSpeed(int frgb, double targetlum, double nowlum, int nowpoint, int nowRegValue, int lastpoint, int lastRegValue, out int nowRegValueAdj)
        {

            if ((nowRegValue > lastRegValue) && (index > 0))
            {
                //nowRegValue = lastRegValue - 1; //之前这里的操作容易引起一个极其隐蔽的随机Bug， 例如：nowRegValue=393，lastRegValue=390，step_tmpr=4
                //此时经过上面的操作后nowRegValue=389，看似正常，但是，再接下来的操作中，某些特殊情况下(计算用的RGB色坐标和实际不符等原因)，nowRegValue会被再次加上（理论上应该是减去）step_tmpr
                //此时，nowRegValue+step_tmpr=389+4，结果，经过一个流程处理后，nowRegValue返回了一个和原来一样值，此时调整的过程就会被卡死~
                nowRegValue = lastRegValue - 1 - step_tmpr;
            }
            if ((teppoint != nowpoint))
            {
                teppoint = nowpoint;
                step_tmpr = Convert.ToInt16(numGammaStep.Value);
                step_tmpg = Convert.ToInt16(numGammaStep.Value);
                step_tmpb = Convert.ToInt16(numGammaStep.Value);
                if (nowlum > targetlum) //初始化调节方向
                {
                    if (frgb == 1) fdirr = true; //向前瞄准目标数据
                    else if (frgb == 2) fdirg = true;
                    else if (frgb == 3) fdirb = true;
                }
                else
                {
                    if (frgb == 1) fdirr = false;
                    else if (frgb == 2) fdirg = false;
                    else if (frgb == 3) fdirb = false;
                }
            }
            nowRegValueAdj = nowRegValue;

            if (nowlum > targetlum)
            {
                //nowdirection = 1;
                //if (lastdirection != nowdirection)
                //{   //统计来回调整的次数
                //    GammaAdjN_COUNT++;
                //    if (GammaAdjN_COUNT > extraInfo)
                //    { textBox1.Text += "调整卡顿\r\n"; }
                //}
                //lastdirection = nowdirection;

                if (frgb == 1)
                {
                    nowdirection = 1;
                    if (lastdirection != nowdirection)
                    {   //统计来回调整的次数
                        GammaAdjN_COUNT++;
                        if (GammaAdjN_COUNT > extraInfo)
                        {
                            textBox1.Text += "R..\r\n";
                            GammaAdjN_COUNT -= 4;
                        }
                    }
                    lastdirection = nowdirection;

                    if (!fdirr)//判断前进方向是否瞄准目标点
                    {
                        if (step_tmpr > 1) step_tmpr >>= 1;
                    }
                    nowRegValueAdj = nowRegValue - step_tmpr;
                    fdirr = true;
                }
                else if (frgb == 2)
                {
                    nowdirectionG = 1;
                    if (lastdirectionG != nowdirectionG)
                    {   //统计来回调整的次数
                        GammaAdjN_COUNTG++;
                        if (GammaAdjN_COUNTG > extraInfo)
                        {
                            textBox1.Text += "G....\r\n";
                            GammaAdjN_COUNTG -= 4;
                        }
                    }
                    lastdirectionG = nowdirectionG;

                    if (!fdirg)
                    {
                        if (step_tmpg > 1) step_tmpg >>= 1;

                    }
                    nowRegValueAdj = nowRegValue - step_tmpg;
                    fdirg = true;
                }
                else if (frgb == 3)
                {
                    nowdirectionB = 1;
                    if (lastdirectionB != nowdirectionB)
                    {   //统计来回调整的次数
                        GammaAdjN_COUNTB++;
                        if (GammaAdjN_COUNTB > extraInfo)
                        {
                            textBox1.Text += "B......\r\n";
                            GammaAdjN_COUNTB -= 4;
                        }
                    }
                    lastdirectionB = nowdirectionB;

                    if (!fdirr)
                    {
                        if (step_tmpb > 1) step_tmpb >>= 1;
                    }

                    nowRegValueAdj = nowRegValue - step_tmpb;
                    fdirb = true;
                }

            }
            else if (nowlum < targetlum)
            {
                //nowdirection = 0;
                //if (lastdirection != nowdirection)
                //{   //统计来回调整的次数
                //    GammaAdjN_COUNT++;
                //    if (GammaAdjN_COUNT > extraInfo)
                //    { textBox1.Text += "调整卡顿\r\n";
                //        GammaAdjN_COUNT = 0;
                //    }
                //}
                //lastdirection = nowdirection;

                if (frgb == 1)
                {
                    nowdirection = 0;
                    if (lastdirection != nowdirection)
                    {   //统计来回调整的次数
                        GammaAdjN_COUNT++;
                        if (GammaAdjN_COUNT > extraInfo)
                        {
                            textBox1.Text += "R..\r\n";
                            GammaAdjN_COUNT = 0;
                        }
                    }
                    lastdirection = nowdirection;

                    if (fdirr)
                    {
                        if ((step_tmpr > 1)) step_tmpr >>= 1;
                    }
                    nowRegValueAdj = nowRegValue + step_tmpr;
                    fdirr = false;
                }
                else if (frgb == 2)
                {
                    nowdirectionG = 0;
                    if (lastdirectionG != nowdirectionG)
                    {   //统计来回调整的次数
                        GammaAdjN_COUNTG++;
                        if (GammaAdjN_COUNTG > extraInfo)
                        {
                            textBox1.Text += "G....\r\n";
                            GammaAdjN_COUNTG -= 4;
                        }
                    }
                    lastdirectionG = nowdirectionG;

                    if (fdirg)
                    {
                        if ((step_tmpg > 1)) step_tmpg >>= 1;
                    }
                    nowRegValueAdj = nowRegValue + step_tmpg;
                    fdirg = false;
                }
                else if (frgb == 3)
                {
                    nowdirectionB = 0;
                    if (lastdirectionB != nowdirectionB)
                    {   //统计来回调整的次数
                        GammaAdjN_COUNTB++;
                        if (GammaAdjN_COUNTB > extraInfo)
                        {
                            textBox1.Text += "B......\r\n";
                            GammaAdjN_COUNTB -= 4;
                        }
                    }
                    lastdirectionB = nowdirectionB;

                    if (fdirb)
                    {
                        if ((step_tmpb > 1)) step_tmpb >>= 1;
                    }
                    nowRegValueAdj = nowRegValue + step_tmpb;
                    fdirb = false;
                }
            }
            //frgb = 0;
            if (nowRegValueAdj > 0xffe)
            {
                nowRegValueAdj = 0xff0;
                textBox1.Text += "Over 0xFFF.. \r\n";
                textBox1.Select(textBox1.Text.Length, 0);
                textBox1.ScrollToCaret();
            }
            else if (nowRegValueAdj < 1)
            {
                nowRegValueAdj = 10;
                textBox1.Text += "GammaValue <1... \r\n";
                textBox1.Select(textBox1.Text.Length, 0);
                textBox1.ScrollToCaret();
            }

        }
        //------------------------------------------------------------
        byte k = 0;
        byte extraInfo;
        private void T18_Tick(object sender, EventArgs e)   //----Gamma 调整----
        {
            double Lr_t, Lg_t, Lb_t, lumr, lumg, lumb;
            //byte extraInfo;
            Msr_CA310(); //Thread.Sleep(500); Msr_CA310();
                         //获取当前亮度值并确定其是否在规格范围内
            T18.Enabled = false;
            if ((auto_lum < (D_lum[index] + lum_off)) && (auto_lum > (D_lum[index] - lum_off)))//亮度
                flag1 = 0;
            if ((auto_y < (D_y + y_off)) && (auto_y > (D_y - y_off)) && flag1 == 0)//y
                flag2 = 0;
            if ((auto_x < (D_x + x_off)) && (auto_x > (D_x - x_off)) && flag1 == 0 && flag2 == 0)//x
                flag3 = 0;
            ///实时显示色坐标和亮度
            ///
            if (RM692CX_Vis.Checked == true)
            {
                RM692CX_Lum.Text = Convert.ToString(auto_lum);
                RM692CX_X.Text = Convert.ToString(auto_x);
                RM692CX_Y.Text = Convert.ToString(auto_y);
            }
            if ((index > 3) && (index < 20))
            {
                extraInfo = Convert.ToByte(nudGammaAdjTimes.Value * 3);
            }
            else if (((index > 20) && (index < 31)))
            {
                extraInfo = Convert.ToByte(nudGammaAdjTimes.Value * 4);
            }
            else { extraInfo = Convert.ToByte(nudGammaAdjTimes.Value * 4); }
            //#region  //超时处理
            //if (GammaAdjustTimes >= nudGammaAdjTimes.Value)//调整超预定次数，则清除所有调试OK标志
            //{
            //    flag1 = flag2 = flag3 = 1; GammaAdjustTimes = 0;
            //    k++;
            //    if (k > 5) //强行改变当前值
            //    {
            //        k = 0;
            //        RGBValue[index, 0] = RGBValue[index, 0] - 5;
            //        RGBValue[index, 1] = RGBValue[index, 1] - 8;
            //        RGBValue[index, 2] = RGBValue[index, 2] + 3;
            //    }

            //}
            //#endregion
            if ((flag1 == 0 && flag2 == 0 && flag3 == 0) | (gammasetjumpflag) | ((GammaAdjN_COUNT > extraInfo - 3) && (GammaAdjN_COUNTG > extraInfo - 3) && (GammaAdjN_COUNTB > extraInfo - 3)))//增加绑点跳过gammasetjumpflag
            {
                if (((GammaAdjN_COUNT > extraInfo - 3) && (GammaAdjN_COUNTG > extraInfo - 3) && (GammaAdjN_COUNTB > extraInfo - 3)))
                {
                    textBox1.Text += (index + 1) + "绑点被模糊处理\r\n";
                }
                GammaAdjN_COUNT = 0;
                gammasetjumpflag = false;
                textBox1.Text += "绑点" + (index + 1) + "调试OK\r\n";
                //textBox1.Text += "lumoff:" + lum_off + ",xoff:"+x_off + ",yoff:" + y_off + "\r\n";
                //把光标（文本起始点）移动到文件框中，文本的末尾处
                this.textBox1.SelectionStart = this.textBox1.TextLength;
                //然后滚动到光标所在的位置
                this.textBox1.ScrollToCaret();

                index++;
                adjust_Rnum = 0;
                adjust_Gnum = 0;
                adjust_Bnum = 0;
                adjust_l = 0;
                adjust_x = 0;
                adjust_y = 0;
                GammaAdjustTimes = 0;
                if ((index == 30) | (chkGIRGamma.Checked))    //只调32个灰阶，最后一个不处理
                {
                    textBox1.Text = textBox1.Text + "RM692CX gamma调试完毕!\r\n";
                    TimTest.Stop();//停止计时

                    textBox1.Select(textBox1.Text.Length, 0);
                    textBox1.ScrollToCaret();
                    T18.Stop(); return;
                }
                if (index < 30)
                {
                    {   //获取允许的误差范围
                        switch (index)
                        {

                            case 0:
                                { lum_off = Convert.ToDouble(RM692CX_P30off.Text); }
                                break;
                            case 1:
                                { lum_off = Convert.ToDouble(RM692CX_P29off.Text); }
                                break;
                            case 2:
                                { lum_off = Convert.ToDouble(RM692CX_P28off.Text); }
                                break;
                            case 3:
                                { lum_off = Convert.ToDouble(RM692CX_P27off.Text); }
                                break;
                            case 4:
                                { lum_off = Convert.ToDouble(RM692CX_P26off.Text); }
                                break;
                            case 5:
                                { lum_off = Convert.ToDouble(RM692CX_P25off.Text); }
                                break;
                            case 6:
                                { lum_off = Convert.ToDouble(RM692CX_P24off.Text); }
                                break;
                            case 7:
                                { lum_off = Convert.ToDouble(RM692CX_P23off.Text); }
                                break;
                            case 8:
                                { lum_off = Convert.ToDouble(RM692CX_P22off.Text); }
                                break;
                            case 9:
                                { lum_off = Convert.ToDouble(RM692CX_P21off.Text); }
                                break;
                            case 10:
                                { lum_off = Convert.ToDouble(RM692CX_P20off.Text); }
                                break;
                            case 11:
                                { lum_off = Convert.ToDouble(RM692CX_P19off.Text); }
                                break;
                            case 12:
                                { lum_off = Convert.ToDouble(RM692CX_P18off.Text); }
                                break;
                            case 13:
                                { lum_off = Convert.ToDouble(RM692CX_P17off.Text) * Convert.ToDouble(RM692CX_P17.Text) / 100; }
                                break;
                            case 14:
                                { lum_off = Convert.ToDouble(RM692CX_P16off.Text) * Convert.ToDouble(RM692CX_P16.Text) / 100; }
                                break;
                            case 15:
                                { lum_off = Convert.ToDouble(RM692CX_P15off.Text) * Convert.ToDouble(RM692CX_P15.Text) / 100; }
                                break;
                            case 16:
                                { lum_off = Convert.ToDouble(RM692CX_P14off.Text) * Convert.ToDouble(RM692CX_P14.Text) / 100; }
                                break;
                            case 17:
                                { lum_off = Convert.ToDouble(RM692CX_P13off.Text) * Convert.ToDouble(RM692CX_P13.Text) / 100; }
                                break;
                            case 18:
                                { lum_off = Convert.ToDouble(RM692CX_P12off.Text) * Convert.ToDouble(RM692CX_P12.Text) / 100; }

                                break;
                            case 19:

                                { lum_off = Convert.ToDouble(RM692CX_P11off.Text) * Convert.ToDouble(RM692CX_P11.Text) / 100; }

                                break;
                            case 20:

                                { lum_off = Convert.ToDouble(RM692CX_P10off.Text) * Convert.ToDouble(RM692CX_P10.Text) / 100; }

                                break;
                            case 21:
                                {
                                    lum_off = Convert.ToDouble(RM692CX_P9off.Text) * Convert.ToDouble(RM692CX_P9.Text) / 100;
                                    //x_off = x_off + 0.002;
                                    //y_off = y_off + 0.002;
                                }
                                break;
                            case 22:
                                {
                                    lum_off = Convert.ToDouble(RM692CX_P8off.Text) * Convert.ToDouble(RM692CX_P8.Text) / 100;
                                    //x_off = x_off + 0.001;
                                    //y_off = y_off + 0.001;
                                }
                                break;
                            case 23:
                                {
                                    lum_off = Convert.ToDouble(RM692CX_P7off.Text) * Convert.ToDouble(RM692CX_7.Text) / 100;
                                    //x_off = x_off + 0.001;
                                    //y_off = y_off + 0.001;
                                }
                                break;
                            case 24:
                                {
                                    lum_off = Convert.ToDouble(RM692CX_P6off.Text) * Convert.ToDouble(RM692CX_P6.Text) / 100;
                                    //x_off = x_off + 0.001;
                                    //y_off = y_off + 0.001;
                                }
                                break;
                            case 25:
                                {
                                    lum_off = Convert.ToDouble(RM692CX_P5off.Text) * Convert.ToDouble(RM692CX_P5.Text) / 100;
                                    //x_off = x_off + 0.001;
                                    //y_off = y_off + 0.001;
                                }
                                break;
                            case 26:
                                {
                                    lum_off = Convert.ToDouble(RM692CX_P4off.Text) * Convert.ToDouble(RM692CX_P4.Text) / 100;
                                    //x_off = x_off + 0.001;
                                    //y_off = y_off + 0.001;
                                }
                                break;
                            case 27:
                                {
                                    lum_off = Convert.ToDouble(RM692CX_P3off.Text) * Convert.ToDouble(RM692CX_P3.Text) / 100;
                                    //x_off = x_off + 0.001;
                                    //y_off = y_off + 0.001;
                                }
                                break;
                            case 28:
                                {
                                    lum_off = Convert.ToDouble(RM692CX_P2off.Text) * Convert.ToDouble(RM692CX_P2.Text) / 100;
                                    //x_off = x_off + 0.001;
                                    //y_off = y_off + 0.001;
                                }
                                break;
                            case 29:
                                {
                                    lum_off = Convert.ToDouble(RM692CX_P1off.Text) * Convert.ToDouble(RM692CX_P1.Text) / 100;
                                    //x_off = x_off + 0.001;
                                    //y_off = y_off + 0.001;
                                }
                                break;
                            default:

                                break;
                        }
                    }
                    if (Vesa_Check.Checked == true)
                    {
                        RM692Cx_btnPrevPic_Run("A" + Point_692AX[index].ToString() + ".dsc_0001.bmp");
                    }
                    else if ((!chkGIRGamma.Checked))
                    {
                        numPatternR.Value = Point_692AX[index];
                        numPatternG.Value = Point_692AX[index];
                        numPatternB.Value = Point_692AX[index];
                        OLED_Show_Click(btnPatternW, e);//写入Pattern参数
                        labtargetlum.Text = "TL： " + D_lum[index];
                    }
                    Thread.Sleep(10); //1秒=1000毫秒，
                    T18.Enabled = true;
                    flag1 = 1;
                    flag2 = 1;
                    flag3 = 1;
                }
            }
            else
            {   //如果不在规格内则继续调整
                decimal r = 0, g = 0, b = 0;
                //int flexible_gamma_count = 120;
                //int step_tmp;
                Lr_t = R_y * ((auto_y - G_y) * (B_x - auto_x) + (auto_x - G_x) * (auto_y - B_y)) / B_y / ((auto_y - G_y) * (auto_x - R_x) + (auto_x - G_x) * (R_y - auto_y));
                Lb_t = 1.0;
                Lg_t = G_y * ((auto_y - R_y) * (B_x - auto_x) + (auto_x - R_x) * (auto_y - B_y)) / B_y / ((auto_y - R_y) * (auto_x - G_x) + (auto_x - R_x) * (G_y - auto_y));
                lumr = auto_lum * Lr_t / (Lr_t + Lg_t + Lb_t);
                lumg = auto_lum * Lg_t / (Lr_t + Lg_t + Lb_t);
                lumb = auto_lum * Lb_t / (Lr_t + Lg_t + Lb_t);

                #region//----收集Gamma RGB 寄存器信息---
                for (int i = 0; i < 33; i++)
                {
                    RGBValueSet(value2array, i, 0, 0);
                    RGBValueSet(value2array, i, 1, 0);
                    RGBValueSet(value2array, i, 2, 0);
                }
                #endregion
                switch (index)
                {
                    case 0:

                        //if (flag3 == 1)
                        {   //private void GammaUpSpeed(double targetlum, double nowlum, int nowpoint, int nowRegValue,  int lastpoint,int lastRegValue, out int nowRegValueAdj);
                            frgb = 1;
                            GammaUpSpeed(frgb, D_lums[index, 0], lumr, Point_692AX[index], RGBValue[index, 0], Point_692AX[index], RGBValue[index, 0], out int nowRegValueAdj);
                            adjust_x++;
                            if (adjust_x > 50)
                            {
                                //x_off += 0.0005;  //待恢复
                                //adjust_x = 0;
                            }
                            if ((nowRegValueAdj > 0))// && (nowRegValueAdj <= RGBValue[index - 1, 0] + 10))
                            {
                                RGBValue[index, 0] = nowRegValueAdj;    //数据更新至数组
                                RGBValueSet(array2value, index, 0, 0);  //通过数组把数据写回控件 //RM692CX_R29.Value = nowRegValueAdj;
                            }
                            else
                            {
                                T18.Enabled = false;
                                MessageBoxTimeoutA((IntPtr)0, "R自动调节失败，请手动调试", "ERROR", 16, 0, 100);
                                //MessageBox.Show("R 自动调节失败，请手动重试");
                            }
                        }
                        //if (flag2 == 1)
                        {
                            //private void GammaUpSpeed(double targetlum, double nowlum, int nowpoint, int nowRegValue,  int lastpoint,int lastRegValue, out int nowRegValueAdj);
                            frgb = 2;
                            GammaUpSpeed(frgb, D_lums[index, 1], lumg, Point_692AX[index], RGBValue[index, 1], Point_692AX[index - 0], RGBValue[index - 0, 1], out int nowRegValueAdj);
                            adjust_x++;
                            if (adjust_x > 50)
                            {
                                //lum_off *= 1.5;  //待恢复
                                //adjust_x = 0;
                            }
                            if ((nowRegValueAdj > 0))//&& (nowRegValueAdj <= RGBValue[index - 1, 1] + 10))
                            {
                                RGBValue[index, 1] = nowRegValueAdj;    //数据更新至数组
                                RGBValueSet(array2value, index, 1, 0);
                            }
                            else
                            {
                                T18.Enabled = false;
                                MessageBoxTimeoutA((IntPtr)0, "G自动调节失败，请手动调试", "ERROR", 16, 0, 100);
                                //MessageBox.Show("G 自动调节失败，请手动重试");
                            }

                        }
                        //if (flag1 == 1)
                        {
                            frgb = 3;
                            GammaUpSpeed(frgb, D_lums[index, 2], lumb, Point_692AX[index], RGBValue[index, 2], Point_692AX[index - 0], RGBValue[index - 0, 2], out int nowRegValueAdj);
                            adjust_x++;
                            if (adjust_x > 50)
                            {
                                //y_off += 0.0005;   //待恢复
                                //adjust_x = 0;
                            }
                            if ((nowRegValueAdj > 0))// && (nowRegValueAdj <= RGBValue[index - 1, 2] + 10))
                            {
                                RGBValue[index, 2] = nowRegValueAdj;    //数据更新至数组
                                RGBValueSet(array2value, index, 2, 0);
                            }
                            else
                            {
                                T18.Enabled = false;
                                MessageBoxTimeoutA((IntPtr)0, "B自动调节失败，请手动调试", "ERROR", 16, 0, 100);
                                //MessageBox.Show("B 自动调节失败，请手动重试");
                            }

                        }
                        GammaDataRevew(29 - index, (UInt16)RGBValue[index, 0], (UInt16)RGBValue[index, 1], (UInt16)RGBValue[index, 2]);
                        auto_gamma_write(gamma_r[30 - index], (UInt16)RGBValue[index, 0], gamma_g[30 - index], (UInt16)RGBValue[index, 1], gamma_b[30 - index], (UInt16)RGBValue[index, 2]);
                        GammaAdjustTimes++;
                        break;
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                    case 7:
                    case 8:
                    case 9:
                    case 10:
                    case 11:
                    case 12:
                    case 13:
                    case 14:
                    case 15:
                    case 16:
                    case 17:
                    case 18:
                    case 19:
                    case 20:
                    case 21:
                    case 22:
                    case 23:
                    case 24:
                    case 25:
                    case 26:
                    case 27:
                    case 28:
                    case 29:
                        //if ((check29.Checked == true) || (check28.Checked == true))//注意，这里只有有一个勾选，即可完成case3～29的过程
                        {
                            //if (flag3 == 1)
                            {   //private void GammaUpSpeed(double targetlum, double nowlum, int nowpoint, int nowRegValue,  int lastpoint,int lastRegValue, out int nowRegValueAdj);
                                frgb = 1;
                                GammaUpSpeed(frgb, D_lums[index, 0], lumr, Point_692AX[index], RGBValue[index, 0], Point_692AX[index - 1], RGBValue[index - 1, 0], out int nowRegValueAdj);
                                adjust_x++;
                                if (adjust_x > 50)
                                {
                                    //x_off += 0.0005;  //待恢复
                                    //adjust_x = 0;
                                }
                                if ((nowRegValueAdj > 0))// && (nowRegValueAdj <= RGBValue[index - 1, 0] + 10))
                                {
                                    RGBValue[index, 0] = nowRegValueAdj;    //数据更新至数组
                                    RGBValueSet(array2value, index, 0, 0);  //通过数组把数据写回控件 //RM692CX_R29.Value = nowRegValueAdj;
                                }
                                else
                                {
                                    T18.Enabled = false;
                                    MessageBoxTimeoutA((IntPtr)0, "R自动调节失败，请手动调试", "ERROR", 16, 0, 100);
                                    //MessageBox.Show("R 自动调节失败，请手动重试");
                                }
                            }
                            //if (flag2 == 1)
                            {
                                //private void GammaUpSpeed(double targetlum, double nowlum, int nowpoint, int nowRegValue,  int lastpoint,int lastRegValue, out int nowRegValueAdj);
                                frgb = 2;
                                GammaUpSpeed(frgb, D_lums[index, 1], lumg, Point_692AX[index], RGBValue[index, 1], Point_692AX[index - 1], RGBValue[index - 1, 1], out int nowRegValueAdj);
                                adjust_x++;
                                if (adjust_x > 50)
                                {
                                    //lum_off *= 1.5;  //待恢复
                                    //adjust_x = 0;
                                }
                                if ((nowRegValueAdj > 0))//&& (nowRegValueAdj <= RGBValue[index - 1, 1] + 10))
                                {
                                    RGBValue[index, 1] = nowRegValueAdj;    //数据更新至数组
                                    RGBValueSet(array2value, index, 1, 0);
                                }
                                else
                                {
                                    T18.Enabled = false;
                                    MessageBoxTimeoutA((IntPtr)0, "G自动调节失败，请手动调试", "ERROR", 16, 0, 100);
                                    //MessageBox.Show("G 自动调节失败，请手动重试");
                                }

                            }
                            //if (flag1 == 1)
                            {
                                frgb = 3;
                                GammaUpSpeed(frgb, D_lums[index, 2], lumb, Point_692AX[index], RGBValue[index, 2], Point_692AX[index - 1], RGBValue[index - 1, 2], out int nowRegValueAdj);
                                adjust_x++;
                                if (adjust_x > 50)
                                {
                                    //y_off += 0.0005;   //待恢复
                                    //adjust_x = 0;
                                }
                                if ((nowRegValueAdj > 0))// && (nowRegValueAdj <= RGBValue[index - 1, 2] + 10))
                                {
                                    RGBValue[index, 2] = nowRegValueAdj;    //数据更新至数组
                                    RGBValueSet(array2value, index, 2, 0);
                                }
                                else
                                {
                                    T18.Enabled = false;
                                    MessageBoxTimeoutA((IntPtr)0, "B自动调节失败，请手动调试", "ERROR", 16, 0, 100);
                                    //MessageBox.Show("B 自动调节失败，请手动重试");
                                }

                            }
                            GammaDataRevew(30 - index, (UInt16)RGBValue[index, 0], (UInt16)RGBValue[index, 1], (UInt16)RGBValue[index, 2]);
                            auto_gamma_write(gamma_r[30 - index], (UInt16)RGBValue[index, 0], gamma_g[30 - index], (UInt16)RGBValue[index, 1], gamma_b[30 - index], (UInt16)RGBValue[index, 2]);
                            GammaAdjustTimes++;
                        }
                        //else { index++; }
                        break;
                    #region
                    //case 30:
                    //    if (check2.Checked == true)
                    //    {
                    //        if (flag3 == 1)
                    //        {
                    //            if (adjust_Rnum > 10)
                    //            {
                    //                if (lumr > D_lums[index, 0])
                    //                    RM692CX_R2.Value = RM692CX_R2.Value - step_l;
                    //                else RM692CX_R2.Value = RM692CX_R2.Value + step_l;
                    //                adjust_Rnum = 0;
                    //                adjust_x++;
                    //                if (adjust_x > 50)
                    //                {
                    //                    x_off += 0.0005;
                    //                    adjust_x = 0;
                    //                }
                    //            }
                    //            else
                    //            {
                    //                if (lumr > D_lums[index, 0])
                    //                    RM692CX_R2.Value = RM692CX_R2.Value - step_l;
                    //                else RM692CX_R2.Value = RM692CX_R2.Value + step_l;
                    //            }
                    //            adjust_Rnum++;
                    //        }
                    //        if (flag1 == 1)
                    //        {
                    //            if (adjust_Gnum > 10)
                    //            {
                    //                if (lumg > D_lums[index, 1])
                    //                    RM692CX_G2.Value = RM692CX_G2.Value - step_h;
                    //                else RM692CX_G2.Value = RM692CX_G2.Value + step_h; adjust_Gnum = 0;
                    //                adjust_l++;
                    //                if (adjust_l > 50)
                    //                {
                    //                    lum_off *= 1.5;
                    //                    adjust_l = 0;
                    //                }
                    //            }
                    //            else
                    //            {
                    //                if (lumg > D_lums[index, 1])
                    //                    RM692CX_G2.Value = RM692CX_G2.Value - step_l;
                    //                else RM692CX_G2.Value = RM692CX_G2.Value + step_l;
                    //            }
                    //            adjust_Gnum++;
                    //        }
                    //        if (flag2 == 1)
                    //        {
                    //            if (adjust_Bnum > 10)
                    //            {
                    //                if (lumb > D_lums[index, 2])
                    //                    RM692CX_B2.Value = RM692CX_B2.Value - step_h;
                    //                else RM692CX_B2.Value = RM692CX_B2.Value + step_h;
                    //                adjust_Bnum = 0;
                    //                adjust_y++;
                    //                if (adjust_y > 50)
                    //                {
                    //                    y_off += 0.0005;
                    //                    adjust_y = 0;
                    //                }
                    //            }
                    //            else
                    //            {
                    //                if (lumb > D_lums[index, 2])
                    //                    RM692CX_B2.Value = RM692CX_B2.Value - step_l;
                    //                else RM692CX_B2.Value = RM692CX_B2.Value + step_l;
                    //            }
                    //            adjust_Bnum++;
                    //        }
                    //        auto_gamma_write(gamma_r[2], Convert.ToUInt16(RM692CX_R2.Value), gamma_g[2], Convert.ToUInt16(RM692CX_G2.Value), gamma_b[2], Convert.ToUInt16(RM692CX_B2.Value));
                    //    }
                    //    else { index++; }
                    //    break;

                    //case 31:
                    //    if (check1.Checked == true)
                    //    {
                    //        if (flag3 == 1)
                    //        {
                    //            if (adjust_Rnum > 10)
                    //            {
                    //                if (lumr > D_lums[index, 0])
                    //                    RM692CX_R1.Value = RM692CX_R1.Value - step_l;
                    //                else RM692CX_R1.Value = RM692CX_R1.Value + step_l;
                    //                adjust_Rnum = 0;
                    //                adjust_x++;
                    //                if (adjust_x > 50)
                    //                {
                    //                    x_off += 0.0005;
                    //                    adjust_x = 0;
                    //                }
                    //            }
                    //            else
                    //            {
                    //                if (lumr > D_lums[index, 0])
                    //                    RM692CX_R1.Value = RM692CX_R1.Value - step_l;
                    //                else RM692CX_R1.Value = RM692CX_R1.Value + step_l;
                    //            }
                    //            adjust_Rnum++;
                    //        }
                    //        if (flag1 == 1)
                    //        {
                    //            if (adjust_Gnum > 10)
                    //            {
                    //                if (lumg > D_lums[index, 1])
                    //                    RM692CX_G1.Value = RM692CX_G1.Value - step_h;
                    //                else RM692CX_G1.Value = RM692CX_G1.Value + step_h; adjust_Gnum = 0;
                    //                adjust_l++;
                    //                if (adjust_l > 50)
                    //                {
                    //                    lum_off *= 1.5;
                    //                    adjust_l = 0;
                    //                }
                    //            }
                    //            else
                    //            {
                    //                if (lumg > D_lums[index, 1])
                    //                    RM692CX_G1.Value = RM692CX_G1.Value - step_l;
                    //                else RM692CX_G1.Value = RM692CX_G1.Value + step_l;
                    //            }
                    //            adjust_Gnum++;
                    //        }
                    //        if (flag2 == 1)
                    //        {
                    //            if (adjust_Bnum > 10)
                    //            {
                    //                if (lumb > D_lums[index, 2])
                    //                    RM692CX_B1.Value = RM692CX_B1.Value - step_l;
                    //                else RM692CX_B1.Value = RM692CX_B1.Value + step_l;
                    //                adjust_Bnum = 0;
                    //                adjust_y++;
                    //                if (adjust_y > 50)
                    //                {
                    //                    y_off += 0.0005;
                    //                    adjust_y = 0;
                    //                }
                    //            }
                    //            else
                    //            {
                    //                if (lumb > D_lums[index, 2])
                    //                    RM692CX_B1.Value = RM692CX_B1.Value - step_l;
                    //                else RM692CX_B1.Value = RM692CX_B1.Value + step_l;
                    //            }
                    //            adjust_Bnum++;
                    //        }
                    //        auto_gamma_write(gamma_r[1], Convert.ToUInt16(RM692CX_R1.Value), gamma_g[1], Convert.ToUInt16(RM692CX_G1.Value), gamma_b[1], Convert.ToUInt16(RM692CX_B1.Value));
                    //    }
                    //    else { index++; }
                    //    break;
                    #endregion
                    default: break;
                }
                T18.Enabled = true;
            }
        }
        private void btn2AXStop_Click(object sender, EventArgs e)   //--停止Gamma调整--
        {
            TimTest.Stop();//停止计时
            T18.Enabled = false;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;
            adjust_Rnum = 0;
            adjust_Gnum = 0;
            adjust_Bnum = 0;
            adjust_l = 0;
            adjust_x = 0;
            adjust_y = 0;

            R_x = 0.6756;//
            R_y = 0.3222;
            G_x = 0.2129;
            G_y = 0.7378;
            B_x = 0.1412;
            B_y = 0.0356;
        }
        bool lumdataReadyFlag;
        private void btnVerfyGamma_Click(object sender, EventArgs e)//Gamma复盘
        {
            int i; double Lumoffset;
            lumdataReadyFlag = false;
            CAS200_txt1.Text = "100";
            cbx_follow_gamma_points.Checked = true;
            //btn_CAS200_Synt_Click(btn_CAS200_Start,e);  //依据绑点量测数据
            //while (lumdataReadyFlag==false) ;
            Lumoffset = Convert.ToDouble(txt2AX_lumoff.Text);
            textBox1.Text = "";
            textBox1.Text += "Gamma校验开始\r\n";
            for (i = 0; i < 30; i++)//(仅仅比对第0-29绑点，最后一个绑点不不比对)
            {
                if ((lumRead[i] < D_lum[i + 2] - Lumoffset) || (lumRead[i] > D_lum[i + 2] + Lumoffset))
                {
                    textBox1.Text += "绑点" + (i + 1) + "可能异常，请确认\r\n";
                    textBox1.SelectionStart = textBox1.TextLength;
                    textBox1.ScrollToCaret();
                }
            }
            textBox1.Text += "Gamma校验结束\r\n";
            textBox1.SelectionStart = textBox1.TextLength;
            textBox1.ScrollToCaret();
        }
        #endregion
        #region //--------------------------亮度量测----
        private int CAS200_Measure_Gray = 255;
        private int CAS200_Measure_Count = 0;
        private double[] lumRead = new double[4096]; //暂存亮度量测数据
        private void T3_Tick(object sender, EventArgs e)    //通过此定时器实现亮度量测
        {
            Int32 step;
            if (chk51step16.Checked)
            {
                step = (Convert.ToUInt16(CAS200_MeasureStep.Text) << 4); //因为滚动条最大值为4095， (4095+1)/(255+1)=16
            }
            else
            {
                step = (Convert.ToUInt16(CAS200_MeasureStep.Text)); //
            }

            if (cbx_follow_51_points.Checked == true)
            {
                if ((MainForm.IC_comboBox.SelectedIndex == 0) || (MainForm.IC_comboBox.SelectedIndex == 4) || (MainForm.IC_comboBox.SelectedIndex == 5) || (MainForm.IC_comboBox.SelectedIndex == 10)) //rm67195
                {
                }
                //else if (MainForm.IC_comboBox.SelectedIndex == 24)//RM692CX
                {

                    if (CAS200_Measure_Gray > 0)
                    {
                        RM692CXHsb_51.Value = CAS200_Measure_Gray - 1;
                    }
                    else
                    {
                        RM692CXHsb_51.Value = 0;
                    }
                    RM692CX_51Set((UInt16)RM692CXHsb_51.Value);
                    R692AX_51Reg.Text = CAS200_Measure_Gray.ToString() + " 阶";
                }
                CAS200_Measure_Gray -= step;
                if (CAS200_Measure_Gray < 0)
                {
                    T3.Enabled = false;
                }
            }
            else if (cbx_follow_gamma_points.Checked == true)  //follow gamma绑点
            {
                T3.Enabled = false;
                if (Vesa_Check.Checked == true)
                {
                    if (Point_692AX[0] == 255)
                    {
                        RM692Cx_btnPrevPic_Run("A" + Point_692AX[CAS200_Measure_Count] + ".dsc_0001.bmp");
                    }
                    else
                    { RM692Cx_btnPrevPic_Run("A" + Point_692AX[CAS200_Measure_Count + 2] + ".dsc_0001.bmp"); }
                }
                else
                {
                    if (Point_692AX[0] == 255)
                    {
                        numPatternB.Value = numPatternG.Value = numPatternR.Value = Point_692AX[CAS200_Measure_Count];
                    }
                    else
                    {
                        //从第三笔数据开始
                        numPatternB.Value = numPatternG.Value = numPatternR.Value = Point_692AX[CAS200_Measure_Count + 2];
                    }
                    Pattern_Write((byte)(numPatternR.Value), (byte)(numPatternG.Value), (byte)(numPatternB.Value));
                }
                //CAS200_Measure_Count++;
                if (CAS200_Measure_Count > 29)
                {
                    T3.Enabled = false;
                    textBox1.Text += "绑点数据量测完毕\r\n";
                    lumdataReadyFlag = true;
                }
                else
                {
                    T3.Enabled = true;
                }

            }
            else
            {
                if ((Convert.ToInt16(numPatternR.Value.ToString()) + Convert.ToInt16(numPatternB.Value.ToString()) + Convert.ToInt16(numPatternG.Value.ToString()) - int.Parse(CAS200_MeasureStep.Text)) >= 0)
                {
                    if (numPatternR.Value * numPatternG.Value * numPatternB.Value == 0)
                    {
                        if (numPatternR.Value != 0)
                        {
                            numPatternR.Value = (byte)(Convert.ToInt16(numPatternR.Value.ToString()) - int.Parse(CAS200_MeasureStep.Text));
                        }
                        if (numPatternG.Value != 0)
                        {
                            numPatternG.Value = (byte)(Convert.ToInt16(numPatternG.Value.ToString()) - int.Parse(CAS200_MeasureStep.Text));
                        }
                        if (numPatternB.Value != 0)
                        {
                            numPatternB.Value = (byte)(Convert.ToInt16(numPatternB.Value.ToString()) - int.Parse(CAS200_MeasureStep.Text));
                        }
                    }
                    else
                    {
                        numPatternB.Value = numPatternG.Value = numPatternR.Value = (byte)(Convert.ToInt16(numPatternR.Value.ToString()) - int.Parse(CAS200_MeasureStep.Text));
                    }
                    if (Vesa_Check.Checked == true)
                    {
                        RM692Cx_btnPrevPic_Run("A" + numPatternB.Value + ".dsc_0001.bmp");
                    }
                    else
                    {
                        Pattern_Write((byte)(numPatternR.Value), (byte)(numPatternG.Value), (byte)(numPatternB.Value));
                    }

                }
                else
                {
                    T3.Enabled = false;
                }
            }
            Thread.Sleep(50);
            btn_CAS200_Synt_Click(btn_CAS200_MeasureOnce, null); //量测
            lumRead[CAS200_Measure_Count] = auto_lum;//亮度数据保存备用
            CAS200_Measure_Count++;
            if ((cbx_follow_51_points.Checked == true) && (CAS200_Measure_Gray <= 0))//51调光时，如果调到最后一阶,则测量后再把51调回去
            {
                RM692CX_51Set((UInt16)(RM692CXHsb_51.Maximum / 2));
            }
        }
        private void btn_CAS200_Synt_Click(object sender, EventArgs e)  //亮度量测：测量 保存 清除 删除 停止 开始
        {
            Button btnTmp = (Button)sender;
            switch (btnTmp.Name)
            {
                case "btn_CAS200_MeasureOnce":
                    {
                        Msr_CA310();
                        CAS200_Measure_Information(auto_lum, auto_x, auto_y);
                    }
                    break;
                case "btn_CAS200_Save":
                    {   //保存
                        SaveFileDialog sf = new SaveFileDialog(); //对话框
                        sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义保存的文件的类型

                        string str = "序号,x坐标,y坐标,亮度\r\n";
                        if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
                        {
                            if (sf.FileName == "") return; //如果没有输入文件名
                            try
                            {
                                StreamWriter sw = null; //文件流
                                sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);
                                for (int i = 0; i < (this.CAS200_dataGridView.Rows.Count - 1); i++)
                                {
                                    str = str + this.CAS200_dataGridView.Rows[i].Cells[0].Value.ToString() + ","
                                        + this.CAS200_dataGridView.Rows[i].Cells[1].Value.ToString() + ","
                                        + this.CAS200_dataGridView.Rows[i].Cells[2].Value.ToString() + ","
                                        + this.CAS200_dataGridView.Rows[i].Cells[3].Value.ToString() + "\r\n";

                                }
                                sw.WriteLine(str);
                                sw.Dispose(); //释放资源
                                sw.Close(); //关闭数据流
                                MessageBox.Show("CA310数据保存OK！", "系统提示：");
                            }
                            catch
                            {
                                MessageBox.Show("CA310数据保存时发生错误，请再试！", "系统提示：");
                            }
                        }
                    }
                    break;
                case "btn_CAS200_Load":
                    {
                        OpenFileDialog sf = new OpenFileDialog();
                        int Line_Count = 0;
                        string strtmp = "";
                        sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义文件的类型
                        sf.Title = "数据导入";
                        if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
                        {
                            FileStream fs = new FileStream(sf.FileName, FileMode.Open, FileAccess.Read);
                            StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
                            streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                            string strLine = streamReader.ReadLine();
                            while (!string.IsNullOrEmpty(strLine))
                            {
                                strtmp = strtmp + strLine + ",";
                                strLine = streamReader.ReadLine(); //循环读取下一行
                                Line_Count++;
                            }
                            fs.Close(); //关闭流
                            streamReader.Close();
                            string[] arrtmp = strtmp.Split(new Char[] { ',' });
                            for (int j = 1; j < Line_Count - 1; j++)
                            {
                                DataGridViewRow Row1 = new DataGridViewRow();
                                CAS200_dataGridView.Rows.Add(Row1);
                            }
                            try
                            {
                                for (int i = 0; i < Line_Count - 1; i++)//有一行标题
                                {
                                    CAS200_dataGridView.Rows[i].Cells[0].Value = arrtmp[4 * (i + 1) + 0];
                                    CAS200_dataGridView.Rows[i].Cells[1].Value = arrtmp[4 * (i + 1) + 1];
                                    CAS200_dataGridView.Rows[i].Cells[2].Value = arrtmp[4 * (i + 1) + 2];
                                    CAS200_dataGridView.Rows[i].Cells[3].Value = arrtmp[4 * (i + 1) + 3];
                                }
                                MessageBox.Show("CA310数据导入OK！", "提示：");
                            }
                            catch
                            {
                                MessageBox.Show("CA310数据导入时发生错误！", "提示：");
                            }
                        }
                    }
                    break;
                case "btn_CAS200_Clean":
                    {
                        DialogResult dr = MessageBox.Show("确认清空列表数据吗？", "系统提示", MessageBoxButtons.OKCancel);
                        if (dr == DialogResult.OK)
                        {
                            this.CAS200_dataGridView.Rows.Clear();
                        }
                        else {; }
                    }
                    break;
                case "btn_CAS200_Delect":
                    {
                        int Current_Index, Row_Accum;
                        Current_Index = this.CAS200_dataGridView.SelectedCells[0].RowIndex;
                        DataGridViewRow Row1 = CAS200_dataGridView.Rows[Current_Index];
                        this.CAS200_dataGridView.Rows.Remove(Row1);
                        this.CAS200_dataGridView.Rows[Current_Index].Selected = false;
                        Row_Accum = this.CAS200_dataGridView.Rows.Count - 1;
                        for (Current_Index = 0; Current_Index < Row_Accum; Current_Index++)
                        {
                            this.CAS200_dataGridView.Rows[Current_Index].Cells[0].Value = Current_Index + 1;//序号
                        }
                        this.CAS200_dataGridView.Rows[Row_Accum - 1].Selected = true;
                        CAS200_dataGridView.FirstDisplayedScrollingRowIndex = CAS200_dataGridView.RowCount - 1;
                    }
                    break;
                case "btn_CAS200_Stop":
                    {
                        T3.Enabled = false;
                    }
                    break;
                case "btn_CAS200_Start":
                    {
                        CAS200_Measure_Count = 0;

                        CAS200_Measure_Gray = RM692CXHsb_51.Value;

                        T3.Enabled = true;
                        T3.Interval = int.Parse(CAS200_txt1.Text);//延时
                        if (cbx_follow_51_points.Checked)
                        {
                            RM692CX_Reg_Data.Text = "fe,00";
                            RM692CX_Others_Click(RM692CX_RegW, e);
                        }
                    }
                    break;
                default: break;
            }
        }
        private void CAS200_MeasureStep_TextChanged(object sender, EventArgs e) //灰阶测量步长调整
        {
            UInt16 i = Convert.ToUInt16(CAS200_MeasureStep.Text);
            if ((i >= 1) && (i <= 8))
            {
                if ((i == 3) | (i == 5))
                {
                    i = 4;
                }
                else if ((i == 6) | (i == 7))
                {
                    i = 8;
                }
            }
            else
            {
                i = 1;
            }

            CAS200_MeasureStep.Text = i.ToString();
        }
        #endregion

        private void btnPage0set_Click(object sender, EventArgs e)      //切换到PAGE0
        {
            RM692CX_Reg_Data.Text = "fe,00";
            RM692CX_Others_Click(RM692CX_RegW, e);
        }
        private void RM692CX_51_Click(object sender, EventArgs e)    //---通过按钮写入51寄存器
        {
            int i;
            string strLine = Convert.ToString(RM692CX_51Reg_W.Text);
            string[] arr = strLine.Split(','); //以逗号分割，添加到数组
            Send_buf = new byte[8];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 8;

            Send_buf[0] = 0x3F;        //3E:RM692CX  
            Send_buf[1] = 0x51;
            Send_buf[2] = 0x03;
            for (i = 0; i < 3; i++)
            {
                Send_buf[3 + i] = byte.Parse((Convert.ToInt32(arr[i], 16)).ToString());
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[6], out Send_buf[7]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 8);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            T1.Enabled = false;
            T1.Interval = 2000;
            T1.Enabled = true;
            ////////临时测试///////////////////////////////////////////////////////
            #region 51两拐点
            /*
            if (checkBox2 .Checked ==true )
            {
                if((Send_buf [4]<=0x03)&&(Send_buf [5]<=08))
                {
                    RM692CX_Reg_Addr.Text = "fe";
                    RM692CX_Reg_Data.Text = "22";
                    RM692CX_Others_Click(RM692CX_Reg_W, e);
                    RM692CX_Reg_Addr.Text = "75";
                    RM692CX_Reg_Data.Text = "ff";
                    RM692CX_Others_Click(RM692CX_Reg_W, e);
                    RM692CX_Reg_Addr.Text = "fe";
                    RM692CX_Reg_Data.Text = "00";
                    RM692CX_Others_Click(RM692CX_Reg_W, e);
                }
                else if ((Send_buf[4] == 0x03) && ((Send_buf [5]<=68)&&(Send_buf[5] >= 08)))
                {
                    RM692CX_Reg_Addr.Text = "fe";
                    RM692CX_Reg_Data.Text = "22";
                    RM692CX_Others_Click(RM692CX_Reg_W, e);
                    RM692CX_Reg_Addr.Text = "75";
                    RM692CX_Reg_Data.Text = "f0";
                    RM692CX_Others_Click(RM692CX_Reg_W, e);
                    RM692CX_Reg_Addr.Text = "fe";
                    RM692CX_Reg_Data.Text = "00";
                    RM692CX_Others_Click(RM692CX_Reg_W, e);
                }
                else if ((Send_buf[4] >= 0x03) && (Send_buf[5] >68))
                {
                    RM692CX_Reg_Addr.Text = "fe";
                    RM692CX_Reg_Data.Text = "22";
                    RM692CX_Others_Click(RM692CX_Reg_W, e);
                    RM692CX_Reg_Addr.Text = "75";
                    RM692CX_Reg_Data.Text = "e0";
                    RM692CX_Others_Click(RM692CX_Reg_W, e);
                    RM692CX_Reg_Addr.Text = "fe";
                    RM692CX_Reg_Data.Text = "00";
                    RM692CX_Others_Click(RM692CX_Reg_W, e);
                }
            }
            */
            #endregion
            //if (Send_buf[4] >= 0x07 && (Send_buf[5] >= 0x69) && checkBox2.Checked == true)
            //{
            //    RM692CX_Reg_Addr.Text = "fe";
            //    RM692CX_Reg_Data.Text = "22";
            //    RM692CX_Others_Click(RM692CX_Reg_W, e);
            //    RM692CX_Reg_Addr.Text = "75";
            //    RM692CX_Reg_Data.Text = "80";
            //    RM692CX_Others_Click(RM692CX_Reg_W, e);
            //    RM692CX_Reg_Addr.Text = "fe";
            //    RM692CX_Reg_Data.Text = "00";
            //    RM692CX_Others_Click(RM692CX_Reg_W, e);
            //}
            //else if ((Send_buf[4] <= 0x07 && Send_buf[5] <= 0x68) && (Send_buf[4] >= 0x06 && Send_buf[5] >= 0xd3) && checkBox2.Checked == true)
            //{
            //    RM692CX_Reg_Addr.Text = "fe";
            //    RM692CX_Reg_Data.Text = "22";
            //    RM692CX_Others_Click(RM692CX_Reg_W, e);
            //    RM692CX_Reg_Addr.Text = "75";
            //    RM692CX_Reg_Data.Text = "90";
            //    RM692CX_Others_Click(RM692CX_Reg_W, e);
            //    RM692CX_Reg_Addr.Text = "fe";
            //    RM692CX_Reg_Data.Text = "00";
            //    RM692CX_Others_Click(RM692CX_Reg_W, e);
            //}
            //else if ((Send_buf[4] == 0x06) && (Send_buf[5] >= 0x3d && Send_buf[5] < 0xd3) && checkBox2.Checked == true)
            //{
            //    RM692CX_Reg_Addr.Text = "fe";
            //    RM692CX_Reg_Data.Text = "22";
            //    RM692CX_Others_Click(RM692CX_Reg_W, e);
            //    RM692CX_Reg_Addr.Text = "75";
            //    RM692CX_Reg_Data.Text = "98";
            //    RM692CX_Others_Click(RM692CX_Reg_W, e);
            //    RM692CX_Reg_Addr.Text = "fe";
            //    RM692CX_Reg_Data.Text = "00";
            //    RM692CX_Others_Click(RM692CX_Reg_W, e);
            //}
            //else if ((Send_buf[4] <= 0x06 && Send_buf[5] < 0x3d) && (Send_buf[4] >= 0x05 && Send_buf[5] >= 0xa7) && checkBox2.Checked == true)
            //{
            //    RM692CX_Reg_Addr.Text = "fe";
            //    RM692CX_Reg_Data.Text = "22";
            //    RM692CX_Others_Click(RM692CX_Reg_W, e);
            //    RM692CX_Reg_Addr.Text = "75";
            //    RM692CX_Reg_Data.Text = "9c";
            //    RM692CX_Others_Click(RM692CX_Reg_W, e);
            //    RM692CX_Reg_Addr.Text = "fe";
            //    RM692CX_Reg_Data.Text = "00";
            //    RM692CX_Others_Click(RM692CX_Reg_W, e);
            //}
            //else if ((Send_buf[4] == 0x05) && (Send_buf[5] < 0xa7 && Send_buf[5] >= 0x11) && checkBox2.Checked == true)
            //{
            //    RM692CX_Reg_Addr.Text = "fe";
            //    RM692CX_Reg_Data.Text = "22";
            //    RM692CX_Others_Click(RM692CX_Reg_W, e);
            //    RM692CX_Reg_Addr.Text = "75";
            //    RM692CX_Reg_Data.Text = "a1";
            //    RM692CX_Others_Click(RM692CX_Reg_W, e);
            //    RM692CX_Reg_Addr.Text = "fe";
            //    RM692CX_Reg_Data.Text = "00";
            //    RM692CX_Others_Click(RM692CX_Reg_W, e);
            //}
            //else if ((Send_buf[4] <= 0x05 && Send_buf[5] < 0xa7) && (Send_buf[4] >= 0x04 && Send_buf[5] >= 0x7b) && checkBox2.Checked == true)
            //{
            //    RM692CX_Reg_Addr.Text = "fe";
            //    RM692CX_Reg_Data.Text = "22";
            //    RM692CX_Others_Click(RM692CX_Reg_W, e);
            //    RM692CX_Reg_Addr.Text = "75";
            //    RM692CX_Reg_Data.Text = "aa";
            //    RM692CX_Others_Click(RM692CX_Reg_W, e);
            //    RM692CX_Reg_Addr.Text = "fe";
            //    RM692CX_Reg_Data.Text = "00";
            //    RM692CX_Others_Click(RM692CX_Reg_W, e);
            //}
            //else if ((Send_buf[4] <= 0x04 && Send_buf[5] < 0x7b) && (Send_buf[4] >= 0x03 && Send_buf[5] >= 0xe5) && checkBox2.Checked == true)
            //{
            //    RM692CX_Reg_Addr.Text = "fe";
            //    RM692CX_Reg_Data.Text = "22";
            //    RM692CX_Others_Click(RM692CX_Reg_W, e);
            //    RM692CX_Reg_Addr.Text = "75";
            //    RM692CX_Reg_Data.Text = "af";
            //    RM692CX_Others_Click(RM692CX_Reg_W, e);
            //    RM692CX_Reg_Addr.Text = "fe";
            //    RM692CX_Reg_Data.Text = "00";
            //    RM692CX_Others_Click(RM692CX_Reg_W, e);
            //}
            //else if ((Send_buf[4] <= 0x03 && Send_buf[5] < 0xaf) && (Send_buf[4] >= 0x02 && Send_buf[5] >= 0xb9) && checkBox2.Checked == true)
            //{
            //    RM692CX_Reg_Addr.Text = "fe";
            //    RM692CX_Reg_Data.Text = "22";
            //    RM692CX_Others_Click(RM692CX_Reg_W, e);
            //    RM692CX_Reg_Addr.Text = "75";
            //    RM692CX_Reg_Data.Text = "b1";
            //    RM692CX_Others_Click(RM692CX_Reg_W, e);
            //    RM692CX_Reg_Addr.Text = "fe";
            //    RM692CX_Reg_Data.Text = "00";
            //    RM692CX_Others_Click(RM692CX_Reg_W, e);
            //}
            //else if ((Send_buf[4] <= 0x02) && (Send_buf[5] < 0xb9 && Send_buf[5] >= 0x23) && checkBox2.Checked == true)
            //{
            //    RM692CX_Reg_Addr.Text = "fe";
            //    RM692CX_Reg_Data.Text = "22";
            //    RM692CX_Others_Click(RM692CX_Reg_W, e);
            //    RM692CX_Reg_Addr.Text = "75";
            //    RM692CX_Reg_Data.Text = "b4";
            //    RM692CX_Others_Click(RM692CX_Reg_W, e);
            //    RM692CX_Reg_Addr.Text = "fe";
            //    RM692CX_Reg_Data.Text = "00";
            //    RM692CX_Others_Click(RM692CX_Reg_W, e);
            //}
            //else if ((Send_buf[4] <= 0x02 && Send_buf[5] < 0x23) && checkBox2.Checked == true)
            //{
            //    RM692CX_Reg_Addr.Text = "fe";
            //    RM692CX_Reg_Data.Text = "22";
            //    RM692CX_Others_Click(RM692CX_Reg_W, e);
            //    RM692CX_Reg_Addr.Text = "75";
            //    RM692CX_Reg_Data.Text = "ff";
            //    RM692CX_Others_Click(RM692CX_Reg_W, e);
            //    RM692CX_Reg_Addr.Text = "fe";
            //    RM692CX_Reg_Data.Text = "00";
            //    RM692CX_Others_Click(RM692CX_Reg_W, e);
            //}
        }
        private void RM692CX_51Set(UInt16 valu51)   //---//直接通过下位机写入51寄存器
        {
            Send_buf = new byte[8];
            Setting.TempRecord.cmdLen = 8;
            valu51 &= 0x0FFF;
            Send_buf[0] = 0x3F;        //RM692CX
            Send_buf[1] = 0x51;
            Send_buf[2] = 0x03;
            //for (i = 0; i < 3; i++)
            //{
            //    Send_buf[3 + i] = byte.Parse((Convert.ToInt32(arr[i], 16)).ToString());
            //}
            Send_buf[3] = 0x51;
            Send_buf[4] = (Byte)(valu51 >> 8);
            Send_buf[5] = (Byte)valu51;

            CRCData.CalculateCrc16(Send_buf, out Send_buf[6], out Send_buf[7]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 8);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            T1.Enabled = false;
            T1.Interval = 2000;
            T1.Enabled = true;
        }
        private void trac2AX_Scroll(object sender, EventArgs e)     //Hsb=HscrollBar; RM692CXHsb_51: 51调光的滚动条
        {
            R692AX_51Reg.Text = RM692CXHsb_51.Value.ToString() + "阶";
            if (num51bit.Value < 9)  //8bit 调光
            {
                if (RM692CXHsb_51.Value == 256) RM692CXHsb_51.Value = 255;
                RM692CX_51Reg_W.Text = "51" + "," + RM692CXHsb_51.Value.ToString("x4").Substring(2, 2) + ",0";
            }
            else
            {
                RM692CX_51Reg_W.Text = "51" + "," + RM692CXHsb_51.Value.ToString("x4").Substring(0, 2) + "," + RM692CXHsb_51.Value.ToString("x4").Substring(2, 2);
            }
            RM692CX_51_Click(bW_51, e);
        }

        private void BtnMTPRead_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[27];
            Setting.TempRecord.cmdLen = 27;

            Send_buf[0] = 0x3F;        //RM692CX
            Send_buf[1] = 0x1B;   //Read All MTP Done Flag
            Send_buf[2] = 27 - 5;
            CRCData.CalculateCrc16(Send_buf, out Send_buf[25], out Send_buf[26]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 27);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            T1.Enabled = false;
            T1.Interval = 2000;
            T1.Enabled = true;
        }
        //------------Done Flag Reg =DFR------
        private readonly byte DFRAE = 2; private readonly byte DFRAF = 3; private readonly byte DFRB0 = 4; private readonly byte DFRB1 = 5;
        private readonly byte DFRB4 = 6; private readonly byte DFRB3 = 7; private readonly byte DFRB2 = 8; private readonly byte DFRA9 = 9;
        private readonly byte DFRAA = 10; private readonly byte DFRAB = 11; private readonly byte DFRBA = 12; private readonly byte DFRBB = 13;
        private readonly byte DFRAD = 14; private readonly byte DFRAC = 15; private readonly byte DFRB7 = 16; private readonly byte DFRB6 = 17;
        private readonly byte DFRB5 = 18; private readonly byte DFRB9 = 19; private readonly byte DFRB8 = 20;
        private void MTPDoneFlagShow_Tick(object sender, EventArgs e)
        { ////（Addr[21]={0xe7,0xe6,0xAE ,0xAF ,0xB0 ,0xB1 ,0xB4 ,0xB3 ,0xB2 ,0xA9 ,0xAA,0xAB,0xBA,0xBB,0xAD ,0xAC ,0xB7 ,0xB6,0xB5,0xB9 ,0xB8};）

            MTPDoneFlagShow.Enabled = false;
            string[] text = new String[60];   //这里的60是随便写的

            if ((MTPDoneFlag[0] & 0x01) == 0x01)
            {
                text[45] = "Page-A0 = 1";
            }
            else text[45] = "Page-A0 = 0";

            if ((MTPDoneFlag[1] & 0x02) == 0x02)  //e6
            {
                text[0] = "Page-D0 = 2";
            }
            else if ((MTPDoneFlag[0] & 0x02) == 0x02)  //e7
            {
                text[0] = "Page-D0 = 1";
            }
            else text[0] = "Page-D0 = 0";

            if ((MTPDoneFlag[1] & 1 << 5) > 0)   //E6
            {
                text[1] = "Page-D2 = 2";
            }
            else if ((MTPDoneFlag[0] & 1 << 5) > 0)   //E7
            {
                text[1] = "Page-D2 = 1";
            }
            else
            {
                text[1] = "Page-D2 = 0";        //        
            }

            if ((MTPDoneFlag[DFRAF] & 1 << 0) > 0)
            {
                text[2] = "Page-40 = 2";
            }
            else if ((MTPDoneFlag[DFRAE] & 1 << 0) > 0)//
            {
                text[2] = "Page-40 = 1";
            }
            else
            {
                text[2] = "Page-40 = 0";
            }

            if ((MTPDoneFlag[DFRAF] & 1 << 1) > 0)
            {
                text[3] = "Page-60 = 2";
            }
            else if ((MTPDoneFlag[DFRAE] & 1 << 1) > 0)
            {
                text[3] = "Page-60 = 1";
            }
            else
            {
                text[3] = "Page-60 = 0";  //
            }

            if ((MTPDoneFlag[DFRAF] & 1 << 2) > 0)//-------
            {
                text[4] = "Page-70 = 2";
            }
            else if ((MTPDoneFlag[DFRAE] & 1 << 2) > 0)
            {
                text[4] = "Page-70 = 1";
            }
            else
            {
                text[4] = "Page-70 = 0";    //
            }

            if ((MTPDoneFlag[DFRAF] & 1 << 3) > 0)
            {
                text[5] = "Page-80 = 2";
            }
            else if ((MTPDoneFlag[DFRAE] & 1 << 3) > 0)
            {
                text[5] = "Page-80 = 1";
            }
            else
            {
                text[5] = "Page-80 = 0";
            }

            if ((MTPDoneFlag[DFRAF] & 1 << 4) > 0)
            {
                text[6] = "Page-90 = 2";
            }
            else if ((MTPDoneFlag[DFRAE] & 1 << 4) > 0)
            {
                text[6] = "Page-90 = 1";
            }
            else
            {
                text[6] = "Page-90 = 0";    //
            }

            if ((MTPDoneFlag[DFRAF] & 1 << 5) > 0)
            {
                text[7] = "Page-E0 = 2";
            }
            else if ((MTPDoneFlag[DFRAE] & 1 << 5) > 0)
            {
                text[7] = "Page-E0 = 1";
            }
            else
            {
                text[7] = "Page-E0 = 0";
            }

            if ((MTPDoneFlag[DFRAF] & 1 << 6) > 0)
            {
                text[8] = "Page-E2 = 2";
            }
            else if ((MTPDoneFlag[DFRAE] & 1 << 6) > 0)
            {
                text[8] = "Page-E2 = 1";
            }
            else
            {
                text[8] = "Page-E2 = 0";
            }

            if ((MTPDoneFlag[DFRAF] & 1 << 7) > 0)
            {
                text[9] = "Page-10 = 2";
            }
            else if ((MTPDoneFlag[DFRAF] & 1 << 7) > 0)  //
            {
                text[9] = "Page-10 = 1";
            }
            else
            {
                text[9] = "Page-10 = 0";    //
            }

            if ((MTPDoneFlag[DFRB1] & 1 << 0) > 0)
            {
                text[10] = "Page-12 = 2";
            }
            else if ((MTPDoneFlag[DFRB0] & 1 << 0) > 0)
            {
                text[10] = "Page-12 = 1";
            }
            else
            {
                text[10] = "Page-12 = 0";
            }

            if ((MTPDoneFlag[DFRB1] & 1 << 6) > 1)
            {
                text[11] = "Page-20 = 3";
            }
            else if ((MTPDoneFlag[DFRB1] & 1 << 1) > 0)
            {
                text[11] = "Page-20 = 2";
            }
            else if ((MTPDoneFlag[DFRB0] & 1 << 1) > 0)
            {
                text[11] = "Page-20 = 1";
            }
            else
            {
                text[11] = "Page-20 = 0";
            }


            if ((MTPDoneFlag[DFRB1] & 1 << 2) > 0)
            {
                text[12] = "Page-22 = 2";
            }
            else if ((MTPDoneFlag[DFRB0] & 1 << 2) > 0)
            {
                text[12] = "Page-22 = 1";
            }
            else
            {
                text[12] = "Page-22 = 0";
            }

            if ((MTPDoneFlag[DFRB1] & 1 << 3) > 0)
            {
                text[13] = "Page-24 = 2";
            }
            else if ((MTPDoneFlag[DFRB0] & 1 << 3) > 0)
            {
                text[13] = "Page-24 = 1";
            }
            else
            {
                text[13] = "Page-24 = 0";
            }

            if ((MTPDoneFlag[DFRB1] & 1 << 4) > 0)
            {
                text[14] = "Page-26 = 2";
            }
            else if ((MTPDoneFlag[DFRB0] & 1 << 4) > 0)
            {
                text[14] = "Page-26 = 1";
            }
            else
            {
                text[14] = "Page-26 = 0";
            }

            if ((MTPDoneFlag[DFRB1] & 1 << 5) > 0)
            {
                text[15] = "Page-72 = 2";
            }
            else if ((MTPDoneFlag[DFRB0] & 1 << 5) > 0)
            {
                text[15] = "Page-72 = 1";
            }
            else
            {
                text[15] = "Page-72 = 0";        //---
            }

            if ((MTPDoneFlag[DFRB2] & 1 << 0) > 0)
            {
                text[16] = "GAM1-50 = 3";
            }
            else if ((MTPDoneFlag[DFRB3] & 1 << 0) > 0)
            {
                text[16] = "GAM1-50 = 2";
            }
            else if ((MTPDoneFlag[DFRB4] & 1 << 0) > 0)
            {
                text[16] = "GAM1-50 = 1";
            }
            else
            {
                text[16] = "GAM1-50 = 0";
            }

            if ((MTPDoneFlag[DFRB2] & 1 << 1) > 0)
            {
                text[17] = "GAM2-52 = 3";
            }
            else if ((MTPDoneFlag[DFRB3] & 1 << 1) > 0)
            {
                text[17] = "GAM2-52 = 2";
            }
            else if ((MTPDoneFlag[DFRB4] & 1 << 1) > 0)
            {
                text[17] = "GAM2-52 = 1";
            }
            else
            {
                text[17] = "GAM2-52 = 0";
            }

            if ((MTPDoneFlag[DFRB2] & 1 << 2) > 0)
            {
                text[18] = "GAM3-54 = 3";
            }
            else if ((MTPDoneFlag[DFRB3] & 1 << 2) > 0)
            {
                text[18] = "GAM3-54 = 2";
            }
            else if ((MTPDoneFlag[DFRB4] & 1 << 2) > 0)
            {
                text[18] = "GAM3-54 = 1";
            }
            else
            {
                text[18] = "GAM3-54 = 0";
            }

            if ((MTPDoneFlag[DFRB2] & 1 << 3) > 0)
            {
                text[19] = "GAM4-56 = 3";
            }
            else if ((MTPDoneFlag[DFRB3] & 1 << 3) > 0)
            {
                text[19] = "GAM4-56 = 2";
            }
            else if ((MTPDoneFlag[DFRB4] & 1 << 3) > 0)
            {
                text[19] = "GAM4-56 = 1";
            }
            else
            {
                text[19] = "GAM4-56 = 0";
            }

            if ((MTPDoneFlag[DFRB2] & 1 << 4) > 0)
            {
                text[20] = "GAM5-50 = 3";
            }
            else if ((MTPDoneFlag[DFRB3] & 1 << 4) > 0)
            {
                text[20] = "GAM5-50 = 2";
            }
            else if ((MTPDoneFlag[DFRB4] & 1 << 4) > 0)
            {
                text[20] = "GAM5-50 = 1";
            }
            else
            {
                text[20] = "GAM5-50 = 0";
            }

            if ((MTPDoneFlag[DFRB2] & 1 << 5) > 0)
            {
                text[21] = "GAM6-52 = 3";
            }
            else if ((MTPDoneFlag[DFRB3] & 1 << 5) > 0)
            {
                text[21] = "GAM6-52 = 2";
            }
            else if ((MTPDoneFlag[DFRB4] & 1 << 5) > 0)
            {
                text[21] = "GAM6-52 = 1";
            }
            else
            {
                text[21] = "GAM6-52 = 0";
            }

            if ((MTPDoneFlag[DFRB2] & 1 << 6) > 0)
            {
                text[22] = "GAM7-54 = 3";
            }
            else if ((MTPDoneFlag[DFRB3] & 1 << 6) > 0)
            {
                text[22] = "GAM7-54 = 2";
            }
            else if ((MTPDoneFlag[DFRB4] & 1 << 6) > 0)
            {
                text[22] = "GAM7-54 = 1";
            }
            else
            {
                text[22] = "GAM7-54 = 0";
            }

            if ((MTPDoneFlag[DFRB2] & 1 << 7) > 0)
            {
                text[23] = "GAM8-56 = 3";
            }
            else if ((MTPDoneFlag[DFRB3] & 1 << 7) > 0)
            {
                text[23] = "GAM8-56 = 2";
            }
            else if ((MTPDoneFlag[DFRB4] & 1 << 7) > 0)
            {
                text[23] = "GAM8-56 = 1";
            }
            else
            {
                text[23] = "GAM8-56 = 0";
            }
            //---------------------------------------------此处CID CID2  CID3  92 82 14省略了-
            if ((MTPDoneFlag[DFRBB] & 1 << 0) > 0)
            {
                text[24] = "Page-74 = 2";
            }
            else if ((MTPDoneFlag[DFRBA] & 1 << 0) > 0)
            {
                text[24] = "Page-74 = 1";
            }
            else
            {
                text[24] = "Page-74 = 0";
            }

            if ((MTPDoneFlag[DFRBB] & 1 << 1) > 0)
            {
                text[25] = "Page-76 = 2";
            }
            else if ((MTPDoneFlag[DFRBA] & 1 << 1) > 0)
            {
                text[25] = "Page-76 = 1";
            }
            else
            {
                text[25] = "Page-76 = 0";    //---
            }

            if ((MTPDoneFlag[DFRAD] & 1 << 1) > 0)
            {
                text[26] = "Page-F0 = 2";
            }
            else if ((MTPDoneFlag[DFRAD] & 1 << 0) > 0)
            {
                text[26] = "Page-F0 = 1";
            }
            else
            {
                text[26] = "Page-F0 = 0";    //
            }

            if ((MTPDoneFlag[DFRAD] & 1 << 3) > 0)
            {
                text[27] = "Page-F2 = 2";
            }
            else if ((MTPDoneFlag[DFRAD] & 1 << 2) > 0)
            {
                text[27] = "Page-F2 = 1";
            }
            else
            {
                text[27] = "Page-F2 = 0";    //
            }

            if ((MTPDoneFlag[DFRAC] & 1 << 4) > 0)
            {
                text[28] = "LIR1-30 = 2";
            }
            else if ((MTPDoneFlag[DFRAC] & 1 << 0) > 0)
            {
                text[28] = "LIR1-30 = 1";
            }
            else
            {
                text[28] = "LIR1-30 = 0";
            }


            if ((MTPDoneFlag[DFRAC] & 1 << 5) > 0)
            {
                text[29] = "LIR1-32 = 2";
            }
            else if ((MTPDoneFlag[DFRAC] & 1 << 1) > 0)
            {
                text[29] = "LIR1-32 = 1";
            }
            else
            {
                text[29] = "LIR1-32 = 0";
            }

            if ((MTPDoneFlag[DFRAC] & 1 << 6) > 0)
            {
                text[30] = "LIR1-34 = 2";
            }
            else if ((MTPDoneFlag[DFRAC] & 1 << 2) > 0)
            {
                text[30] = "LIR1-34 = 1";
            }
            else
            {
                text[30] = "LIR1-34 = 0";
            }

            if ((MTPDoneFlag[DFRAC] & 1 << 7) > 0)
            {
                text[31] = "LIR1-36 = 2";
            }
            else if ((MTPDoneFlag[DFRAC] & 1 << 3) > 0)
            {
                text[31] = "LIR1-36 = 1";
            }
            else
            {
                text[31] = "LIR1-36 = 0";    //
            }


            if ((MTPDoneFlag[DFRB5] & 1 << 0) > 0)
            {
                text[32] = "Gamma9-50 = 3";
            }
            else if ((MTPDoneFlag[DFRB6] & 1 << 0) > 0)
            {
                text[32] = "Gamma9-50 = 2";
            }
            else if ((MTPDoneFlag[DFRB7] & 1 << 0) > 0)
            {
                text[32] = "Gamma9-50 = 1";
            }
            else
            {
                text[32] = "Gamma9-50 = 0";
            }

            if ((MTPDoneFlag[DFRB5] & 1 << 1) > 0)
            {
                text[32] = "Gamma10-52 = 3";
            }
            else if ((MTPDoneFlag[DFRB6] & 1 << 1) > 0)
            {
                text[32] = "Gamma10-52 = 2";
            }
            else if ((MTPDoneFlag[DFRB7] & 1 << 1) > 0)
            {
                text[32] = "Gamma10-52 = 1";
            }
            else
            {
                text[32] = "Gamma10-52 = 0";
            }

            if ((MTPDoneFlag[DFRB5] & 1 << 2) > 0)
            {
                text[33] = "Gamma11-54 = 3";
            }
            else if ((MTPDoneFlag[DFRB6] & 1 << 2) > 0)
            {
                text[33] = "Gamma11-54 = 2";
            }
            else if ((MTPDoneFlag[DFRB7] & 1 << 2) > 0)
            {
                text[33] = "Gamma11-54 = 1";
            }
            else
            {
                text[33] = "Gamma11-54 = 0";
            }

            if ((MTPDoneFlag[DFRB5] & 1 << 3) > 0)
            {
                text[1] = "Gamma12-56 = 3";
            }
            else if ((MTPDoneFlag[DFRB6] & 1 << 3) > 0)
            {
                text[34] = "Gamma12-56 = 2";
            }
            else if ((MTPDoneFlag[DFRB7] & 1 << 3) > 0)
            {
                text[34] = "Gamma12-56 = 1";
            }
            else
            {
                text[34] = "Gamma12-56 = 0";
            }

            if ((MTPDoneFlag[DFRB5] & 1 << 4) > 0)
            {
                text[35] = "Gamma13-50 = 3";
            }
            else if ((MTPDoneFlag[DFRB6] & 1 << 4) > 0)
            {
                text[35] = "Gamma13-50 = 2";
            }
            else if ((MTPDoneFlag[DFRB7] & 1 << 4) > 0)
            {
                text[35] = "Gamma13-50 = 1";
            }
            else
            {
                text[35] = "Gamma13-50 = 0";
            }

            if ((MTPDoneFlag[DFRB5] & 1 << 5) > 0)
            {
                text[36] = "Gamma14-52 = 3";
            }
            else if ((MTPDoneFlag[DFRB6] & 1 << 5) > 0)
            {
                text[36] = "Gamma14-52 = 2";
            }
            else if ((MTPDoneFlag[DFRB7] & 1 << 5) > 0)
            {
                text[36] = "Gamma14-52 = 1";
            }
            else
            {
                text[36] = "Gamma14-52 = 0";
            }


            if ((MTPDoneFlag[DFRB6] & 1 << 6) > 0)
            {
                text[37] = "Gamma15-54 = 2";
            }
            else if ((MTPDoneFlag[DFRB7] & 1 << 6) > 0)
            {
                text[37] = "Gamma15-54 = 1";
            }
            else
            {
                text[37] = "Gamma15-54 = 0";
            }

            if ((MTPDoneFlag[DFRB6] & 1 << 7) > 0)
            {
                text[38] = "Gamma16-56 = 2";
            }
            else if ((MTPDoneFlag[DFRB7] & 1 << 7) > 0)
            {
                text[38] = "Gamma16-56 = 1";
            }
            else
            {
                text[38] = "Gamma16-56 = 0"; //
            }

            if ((MTPDoneFlag[DFRB8] & 1 << 0) > 0)
            {
                text[39] = "Gamma17-50 = 2";
            }
            else if ((MTPDoneFlag[DFRB9] & 1 << 0) > 0)
            {
                text[39] = "Gamma17-50 = 1";
            }
            else
            {
                text[39] = "Gamma17-50 = 0";
            }

            if ((MTPDoneFlag[DFRB8] & 1 << 1) > 0)
            {
                text[40] = "Gamma18-52 = 2";
            }
            else if ((MTPDoneFlag[DFRB9] & 1 << 1) > 0)
            {
                text[40] = "Gamma18-52 = 1";
            }
            else
            {
                text[40] = "Gamma18-52 = 0";
            }

            if ((MTPDoneFlag[DFRB8] & 1 << 2) > 0)
            {
                text[41] = "Gamma19-54 = 2";
            }
            else if ((MTPDoneFlag[DFRB9] & 1 << 2) > 0)
            {
                text[41] = "Gamma19-54 = 1";
            }
            else
            {
                text[41] = "Gamma19-54 = 0";
            }

            if ((MTPDoneFlag[DFRB8] & 1 << 3) > 0)
            {
                text[42] = "Gamma20-56 = 2";
            }
            else if ((MTPDoneFlag[DFRB9] & 1 << 3) > 0)
            {
                text[42] = "Gamma20-56 = 1";
            }
            else
            {
                text[42] = "Gamma20-56 = 0";
            }

            if ((MTPDoneFlag[DFRB8] & 1 << 4) > 0)
            {
                text[43] = "Gamma21-50 = 2";
            }
            else if ((MTPDoneFlag[DFRB9] & 1 << 4) > 0)
            {
                text[43] = "Gamma21-50 = 1";
            }
            else
            {
                text[43] = "Gamma21-50 = 0";
            }

            if ((MTPDoneFlag[DFRB0] & 1 << 7) > 0)
            {
                text[44] = "Page-RCN = 2";
            }
            else if ((MTPDoneFlag[DFRB0] & 1 << 6) > 0)
            {
                text[44] = "Page-RCN = 1";
            }
            else
            {
                text[44] = "Page-RCN = 0";
            }

            txtRegStatusShow.Text = "";
            for (int i = 0; i < 46; i++)
            {
                txtRegStatusShow.AppendText((text[i]) + "\r\n");
            }

        }
        public void RM692CX_OTP_Read(object sender, EventArgs e)     //读取所有PAGE页的OTP次数       
        {
            Send_buf = new byte[17];

            Setting.TempRecord.cmdLen = 17;

            Send_buf[0] = 0x3F;        //RM692CX
            Send_buf[1] = 0x0B;
            Send_buf[2] = 12;
            CRCData.CalculateCrc16(Send_buf, out Send_buf[15], out Send_buf[16]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 17);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            T1.Enabled = false;
            T1.Interval = 2000;
            T1.Enabled = true;
            Application.DoEvents();
            RM692CX_OTPRegStatusShow();
        }
        private void RM692CX_OTPRegStatusShow()
        {
            string[] text = new String[28];
            #region 
            //GAMMA_1
            if ((OTP_Time_Data[2] & 0x01) == 0x01)
            {
                text[0] = "GAMMA_1 = 3";
                //textBox1.Text = "GAMMA_1 = 3\r\n";
            }
            else if ((OTP_Time_Data[1] & 0x01) == 0x01)
            {
                text[0] = "GAMMA_1 = 2";
                //textBox1.Text = "GAMMA_1 = 2\r\n";
            }
            else if ((OTP_Time_Data[0] & 0x01) == 0x01)
            {
                text[0] = "GAMMA_1 = 1";
                //textBox1.Text = "GAMMA_1 = 1\r\n";
            }
            else
            {
                text[0] = "GAMMA_1 = 0";
                //textBox1.Text = "GAMMA_1 = 0\r\n";
            }
            //GAMMA_2
            if ((OTP_Time_Data[2] & 0x02) == 0x02)
            {
                text[1] = "GAMMA_2 = 3";
                //textBox1.Text = "GAMMA_2 = 3\r\n";
            }
            else if ((OTP_Time_Data[1] & 0x02) == 0x02)
            {
                text[1] = "GAMMA_2 = 2";
                //textBox1.Text = "GAMMA_2 = 2\r\n";
            }
            else if ((OTP_Time_Data[0] & 0x02) == 0x02)
            {
                text[1] = "GAMMA_2 = 1";
                // textBox1.Text = "GAMMA_2 = 1\r\n";
            }
            else
            {
                text[1] = "GAMMA_2 = 0";
                // textBox1.Text = "GAMMA_2 = 0\r\n";
            }
            //GAMMA_3
            if ((OTP_Time_Data[2] & 0x04) == 0x04)
            {
                text[2] = "GAMMA_3 = 3";
                // textBox1.Text = "GAMMA_3 = 3\r\n";
            }
            else if ((OTP_Time_Data[1] & 0x04) == 0x04)
            {
                text[2] = "GAMMA_3 = 2";
                // textBox1.Text = "GAMMA_3 = 2\r\n";
            }
            else if ((OTP_Time_Data[0] & 0x04) == 0x04)
            {
                text[2] = "GAMMA_3 = 1";
                // textBox1.Text = "GAMMA_3 = 1\r\n";
            }
            else
            {
                text[2] = "GAMMA_3 = 0";
                // textBox1.Text = "GAMMA_3 = 0\r\n";
            }

            //GAMMA_4
            if ((OTP_Time_Data[2] & 0x08) == 0x08)
            {
                text[3] = "GAMMA_4 = 3";
                // textBox1.Text = "GAMMA_4 = 3\r\n";
            }
            else if ((OTP_Time_Data[1] & 0x08) == 0x08)
            {
                text[3] = "GAMMA_4 = 2";
                // textBox1.Text = "GAMMA_4 = 2\r\n";
            }
            else if ((OTP_Time_Data[0] & 0x08) == 0x08)
            {
                text[3] = "GAMMA_4 = 1";
                //  textBox1.Text = "GAMMA_4 = 1\r\n";
            }
            else
            {
                text[3] = "GAMMA_4 = 0";
                //  textBox1.Text = "GAMMA_4 = 0\r\n";
            }
            //GAMMA_5
            if ((OTP_Time_Data[2] & 0x10) == 0x10)
            {
                text[4] = "GAMMA_5 = 3";
                //  textBox1.Text = "GAMMA_5 = 3\r\n";
            }
            else if ((OTP_Time_Data[1] & 0x10) == 0x10)
            {
                text[4] = "GAMMA_5 = 2";
                //  textBox1.Text = "GAMMA_5 = 2\r\n";
            }
            else if ((OTP_Time_Data[0] & 0x10) == 0x10)
            {
                text[4] = "GAMMA_5 = 1";
                //  textBox1.Text = "GAMMA_5 = 1\r\n";
            }
            else
            {
                text[4] = "GAMMA_5 = 0";
                // textBox1.Text = "GAMMA_5 = 0\r\n";
            }
            //GAMMA_6
            if ((OTP_Time_Data[2] & 0x20) == 0x20)
            {
                text[5] = "GAMMA_6 = 3";
                //  textBox1.Text = "GAMMA_6 = 3\r\n";
            }
            else if ((OTP_Time_Data[1] & 0x20) == 0x20)
            {
                text[5] = "GAMMA_6 = 2";
                // textBox1.Text = "GAMMA_6 = 2\r\n";
            }
            else if ((OTP_Time_Data[0] & 0x20) == 0x20)
            {
                text[5] = "GAMMA_6 = 1";
                //  textBox1.Text = "GAMMA_6 = 1\r\n";
            }
            else
            {
                text[5] = "GAMMA_6 = 0";
                //  textBox1.Text = "GAMMA_6 = 0\r\n";
            }
            //GAMMA_7
            if ((OTP_Time_Data[2] & 0x40) == 0x40)
            {
                text[6] = "GAMMA_7 = 3";
                //  textBox1.Text = "GAMMA_7 = 3\r\n";
            }
            else if ((OTP_Time_Data[1] & 0x40) == 0x40)
            {
                text[6] = "GAMMA_7 = 2";
                //  textBox1.Text = "GAMMA_7 = 2\r\n";
            }
            else if ((OTP_Time_Data[0] & 0x40) == 0x40)
            {
                text[6] = "GAMMA_7 = 1";
                //  textBox1.Text = "GAMMA_7 = 1\r\n";
            }
            else
            {
                text[6] = "GAMMA_7 = 0";
                //  textBox1.Text = "GAMMA_7 = 0\r\n";
            }
            //GAMMA_8
            if ((OTP_Time_Data[2] & 0x80) == 0x80)
            {
                text[7] = "GAMMA_8 = 3";
                // textBox1.Text = "GAMMA_8 = 3\r\n";
            }
            else if ((OTP_Time_Data[1] & 0x80) == 0x80)
            {
                text[7] = "GAMMA_8 = 2";
                // textBox1.Text = "GAMMA_8 = 2\r\n";
            }
            else if ((OTP_Time_Data[0] & 0x80) == 0x80)
            {
                text[7] = "GAMMA_8 = 1";
                // textBox1.Text = "GAMMA_8 = 1\r\n";
            }
            else
            {
                text[7] = "GAMMA_8 = 0";
                // textBox1.Text = "GAMMA_8 = 0\r\n";
            }
            //GAMMA_9
            if ((OTP_Time_Data[5] & 0x01) == 0x01)
            {
                text[8] = "GAMMA_9 = 3";
                // textBox1.Text = "GAMMA_9 = 3\r\n";
            }
            else if ((OTP_Time_Data[4] & 0x01) == 0x01)
            {
                text[8] = "GAMMA_9 = 2";
                // textBox1.Text = "GAMMA_9 = 2\r\n";
            }
            else if ((OTP_Time_Data[3] & 0x01) == 0x01)
            {
                text[8] = "GAMMA_9 = 1";
                // textBox1.Text = "GAMMA_9 = 1\r\n";
            }
            else
            {
                text[8] = "GAMMA_9 = 0";
                //  textBox1.Text = "GAMMA_9 = 0\r\n";
            }
            //GAMMA_10
            if ((OTP_Time_Data[5] & 0x02) == 0x02)
            {
                text[9] = "GAMMA10 = 3";
                // textBox1.Text = "GAMMA_10 = 3\r\n";
            }
            else if ((OTP_Time_Data[4] & 0x02) == 0x02)
            {
                text[9] = "GAMMA10 = 2";
                //  textBox1.Text = "GAMMA_10 = 2\r\n";
            }
            else if ((OTP_Time_Data[3] & 0x02) == 0x02)
            {
                text[9] = "GAMMA10 = 1";
                // textBox1.Text = "GAMMA_10 = 1\r\n";
            }
            else
            {
                text[9] = "GAMMA10 = 0";
                //textBox1.Text = "GAMMA_10 = 0\r\n";
            }

            txtRegStatusShow.Text = "";
            for (int i = 0; i < 10; i++)
            {
                txtRegStatusShow.AppendText((text[i]) + "\r\n");
            }
            //if(Vesa_Check .Checked ==false )
            //{
            //    Creat_BMP(text);
            //}
        }
        private void Creat_BMP(string[] text)
        {
            #endregion
            ///生成BMP
            ///
            Bitmap bmp2 = new Bitmap(1080, 2340, PixelFormat.Format24bppRgb);                      //设置分辨率及24位深BMP
            for (int i = 0; i < bmp2.Width; i++)
            {
                int gary = 0;
                for (int j = 0; j < bmp2.Height; j++)
                {
                    if (((j % 9) == 0) && (gary < 255))
                    {
                        gary++;
                    }
                    Color c = Color.FromArgb(0 + gary, 0 + gary, 0 + gary);                       //生成灰阶过度图H方向
                    bmp2.SetPixel(i, j, c);
                }
            }
            Graphics g = Graphics.FromImage(bmp2);

            ///打印文字
            ///
            //string text = "1234567890";
            float fontSize = 38f;                       //字体大小设置
            //float textWidth = text.Length * fontSize;
            System.Drawing.Font font = new System.Drawing.Font("加粗", fontSize, FontStyle.Bold);//字型设置

            for (int i = 0; i < text.Length; i++)
            {
                float rectX = bmp2.Width / 10;                                                   //文本框宽度
                float rectY = bmp2.Height / 20 + fontSize * i * 2;                                 //文本框高度
                float rectWidth = (2 * fontSize + font.Height * 2) * 2;                            //文本框在图片上X坐标
                float rectHeight = (fontSize / 72) * 96;                                         //文本框在图片上Y坐标
                RectangleF textArea = new RectangleF(rectX, rectY, rectWidth, rectHeight);

                Brush whiteBrush = new SolidBrush(Color.WhiteSmoke);                            //字体颜色

                Brush blackBrush = new SolidBrush(Color.SlateGray);                            //文本框颜色

                g.FillRectangle(blackBrush, rectX, rectY, rectWidth, rectHeight);

                g.DrawString(text[i], font, whiteBrush, textArea);
            }

            bmp2.Save(System.Windows.Forms.Application.StartupPath + "\\Resources\\OTP_Time.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
            //bmp2.Save(@"C:\\Users\\hanchong\\Desktop\\新建文件夹\\33.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
            bmp2.Dispose();
            RM692Cx_btnPrevPic_Run("OTP_Time.bmp");//发送图片
        }
        private void RM692Cx_btnPrevPic_Run(string filenames)
        {
            Int32 HACT_True = 0;
            Int32 VACT_True = 0;
            Int32 m_txlen = 0;
            Int32 j = 0;
            byte[] TemFileContent = new byte[1024 * 100];
            System.Int32 ByteReaded = 0x00;
            try
            {
                string FileName;
                //string FileName = textBox2.Text + '\\' + listBox3_BMPFile.SelectedItem.ToString();  vesa_bmp_path
                if (Vesa_Check.Checked == true)
                {
                    FileName = vesa_bmp_path + '\\' + filenames;
                }
                else
                {
                    FileName = System.Windows.Forms.Application.StartupPath + "\\Resources\\" + filenames;
                }
                //string FileName = vesa_bmp_path + '\\' + filenames;
                FileStream aFile = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                ByteReaded = aFile.Read(TemFileContent, 0, 54);
                HACT_True = (Convert.ToInt32(TemFileContent[19]) << 8) + TemFileContent[18];
                VACT_True = Convert.ToInt32((TemFileContent[23] << 8) + TemFileContent[22]);
                j = 1;
                //if ((HACT_True == ((TemFileContent[19] << 8) + (TemFileContent[18]))) && (Convert.ToInt32(VACT.Value) == (TemFileContent[23] << 8) + (TemFileContent[22])))//BMP图片水平大小
                {
                    aFile.Seek(54, SeekOrigin.Begin);
                    while (j <= VACT_True)
                    {
                        if (HACT_True % 4 != 0)
                            aFile.Seek((VACT_True - j) * (HACT_True * 3 + (HACT_True % 4)) + 54, SeekOrigin.Begin);
                        else
                            aFile.Seek((VACT_True - j) * HACT_True * 3 + 54, SeekOrigin.Begin);
                        ByteReaded = aFile.Read(TemFileContent, 0, HACT_True * 3); // 
                        try
                        {
                            MainForm.comPort.Write(TemFileContent, 0, ByteReaded);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                            break;
                        }
                        m_txlen += ByteReaded;
                        progressBar1.Value = Convert.ToInt32((double)m_txlen / aFile.Length * 100);
                        label14.Text = progressBar1.Value.ToString() + "%";
                        Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                        j++;
                    }

                    TemFileContent[0] = 0x5A;
                    try
                    {
                        MainForm.comPort.Write(TemFileContent, 0, 1);
                    }
                    catch
                    {
                        MessageBox.Show("串口有误，请检查", "错误");
                    }

                    aFile.Close();
                }
                /*
                else
                {
                    MessageBox.Show("BMP分辨率非法");
                }*/
            }
            catch
            {
                MessageBox.Show("请选择文件夹", "错误");
            }
        }
        string pcstepstr = "";
        private void btnReadReg_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[20];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 20;

            string strLine = "fe,40"; //9a
            string[] arr = strLine.Split(','); //以逗号分割，添加到数组

            Setting.TempRecord.cmdLen = arr.Length + 5;
            Send_buf = new byte[arr.Length + 5];
            Send_buf[0] = 0x3F;        //RM692CX
            Send_buf[1] = 0x01; //寄存器写
            Send_buf[2] = byte.Parse(arr.Length.ToString());
            for (int i = 0; i < arr.Length; i++)
            {
                Send_buf[3 + i] = byte.Parse((Convert.ToInt32(arr[i], 16)).ToString());
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[Send_buf[2] + 3], out Send_buf[Send_buf[2] + 4]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, Send_buf[2] + 5);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
                return;
            }
            Thread.Sleep(1000);

            strLine = "9a";//Convert.ToString(RM692CX_Reg_Data.Text);

            arr = strLine.Split(','); //以逗号分割，添加到数组
            int DataNum = 2;             //Convert.ToByte(tb_RM692CX_return_num.Text);
            Setting.TempRecord.cmdLen = DataNum + 1 + 5;
            Send_buf = new byte[DataNum + 1 + 5];
            Send_buf[0] = 0x3F;        //RM692CX
            Send_buf[1] = 0x02; //read function
            Send_buf[2] = Convert.ToByte(DataNum + 1);//length
            Send_buf[3] = byte.Parse((Convert.ToInt32(arr[0], 16)).ToString());//register               
            CRCData.CalculateCrc16(Send_buf, out Send_buf[Send_buf[2] + 3], out Send_buf[Send_buf[2] + 4]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, Send_buf[2] + 5);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            T1.Enabled = false;
            T1.Interval = 2000;
            T1.Enabled = true;
        }
        //--------------------------------2018.08.09-----寄存器回读----------
        bool ReadedFlag = false;
        byte[] ReadBuf = new byte[2];
        //RM692CX_Others_Click
        private void BtnRegRead_Click(object sender, EventArgs e)
        {
            int i = 0;
        ReRead:
            ReadedFlag = false;
            RM692CX_Reg_Data.Text = "FE";
            RM692CX_Others_Click(BtnHSReadReg, e);
            Application.DoEvents(); Thread.Sleep(20);
            Application.DoEvents();

            while (!ReadedFlag)
            {
                i++;
                if (i >= 10)
                {
                    i = 0;
                    textBox1.Text = "数据读取异常，重试";
                    goto ReRead;
                }
                Application.DoEvents();
            }
            RM692CX_Reg_Data.Text = Rec_tmp[4] + "," + i;
            ReadedFlag = false;
        }
        //--------------------------------------------------------------------
        private void RM692CX_Others_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[50];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 50;

            Send_buf[0] = 0x3F;        //RM692CX
            Send_buf[2] = 0x02;
            switch (btnTmp.Name)
            {
                case "RM692CX_RegR":       //              2
                    {
                        string strLine = Convert.ToString(RM692CX_Reg_Data.Text);
                        string[] arr = strLine.Split(','); //以逗号分割，添加到数组
                        int DataNum = Convert.ToByte(tb_RM692CX_return_num.Text);
                        Setting.TempRecord.cmdLen = DataNum + 1 + 5;
                        Send_buf = new byte[DataNum + 1 + 5];

                        Send_buf[0] = 0x3F;
                        Send_buf[1] = 0x02; //read function
                        Send_buf[2] = Convert.ToByte(DataNum + 1);//length
                        Send_buf[3] = byte.Parse((Convert.ToInt32(arr[0], 16)).ToString());//register
                    }
                    break;
                case "BtnHSReadReg":       //              2
                    {
                        string strLine = Convert.ToString(RM692CX_Reg_Data.Text);
                        string[] arr = strLine.Split(','); //以逗号分割，添加到数组
                        int DataNum = Convert.ToByte(tb_RM692CX_return_num.Text);
                        Setting.TempRecord.cmdLen = DataNum + 1 + 5;
                        Send_buf = new byte[DataNum + 1 + 5];

                        Send_buf[0] = 0x3F;
                        Send_buf[1] = 0x22; //read function
                        Send_buf[2] = Convert.ToByte(DataNum + 1);//length
                        Send_buf[3] = byte.Parse((Convert.ToInt32(arr[0], 16)).ToString());//register
                    }
                    break;
                case "RM692CX_RegW":         //w写寄存器   1
                    {
                        string strLine = Convert.ToString(RM692CX_Reg_Data.Text);
                        string[] arr = strLine.Split(','); //以逗号分割，添加到数组

                        Setting.TempRecord.cmdLen = arr.Length + 5;
                        Send_buf = new byte[arr.Length + 5];
                        Send_buf[0] = 0x3F; //
                        Send_buf[1] = 0x01; //寄存器写
                        Send_buf[2] = byte.Parse(arr.Length.ToString());
                        for (int i = 0; i < arr.Length; i++)
                        {
                            Send_buf[3 + i] = byte.Parse((Convert.ToInt32(arr[i], 16)).ToString());
                        }
                    }
                    break;
                case "OTP_Time_R":       //          B
                    {
                        Send_buf[1] = 0x06;    //      2
                        if (Page50.Checked == true)
                        {
                            Send_buf[3] = 0x00;
                        }
                        else if (Page52.Checked == true)
                        {
                            Send_buf[3] = 0x01;
                        }
                        else if (Page54.Checked == true)
                        {
                            Send_buf[3] = 0x02;
                        }
                        else if (Page56.Checked == true)
                        {
                            Send_buf[3] = 0x03;
                        }
                    }
                    break;
                case "OTP_Time_W":         //w写寄存器   C
                    {
                        if (Convert.ToDecimal(RM692CX_Time.Text) > 4)
                        {
                            MessageBox.Show("OTP次数不能大于4次", "警告");
                            return;
                        }
                        else
                        {
                            Send_buf[1] = 0x07;
                            Send_buf[4] = byte.Parse((Convert.ToInt32(RM692CX_Time.Text.Trim(), 16)).ToString());
                        }
                    }
                    break;

                default:
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[Send_buf[2] + 3], out Send_buf[Send_buf[2] + 4]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, Send_buf[2] + 5);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            T1.Enabled = false;
            T1.Interval = 2000;
            T1.Enabled = true;

        }

        private void RM692CX_OTP_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[9];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 9;

            Send_buf[0] = 0x3F;        //RM692CX
            Send_buf[1] = 0x03;
            Send_buf[2] = 0x04;
            if (Page50.Checked == true)//OTP PAGE50
            {
                Send_buf[6] = 0x0a;
                Send_buf[3] = byte.Parse((Convert.ToInt32(RM692CX_Time.Text.Trim(), 16)).ToString());//OTP次数
                Send_buf[4] = 0x01;  //page 选择
                Send_buf[5] = byte.Parse((Convert.ToInt32(Gamma_Nox.Text.Trim(), 16)).ToString());//Gamma x

                CRCData.CalculateCrc16(Send_buf, out Send_buf[7], out Send_buf[8]);
                try
                {
                    MainForm.comPort.Write(Send_buf, 0, 9);
                }
                catch
                {
                    MessageBox.Show("串口有误，请检查", "错误");
                    return;
                }
                T1.Enabled = false;
                T1.Interval = 2000;
                T1.Enabled = true;
                //Thread.Sleep(200);
                Thread.Sleep(200);
            }
            else if (Page52.Checked == true)
            {
                Send_buf[6] = 0x0d;
                Send_buf[3] = byte.Parse((Convert.ToInt32(RM692CX_Time.Text.Trim(), 16)).ToString());
                Send_buf[4] = 0x02;  //page 选择
                Send_buf[5] = byte.Parse((Convert.ToInt32(Gamma_Nox.Text.Trim(), 16)).ToString());
                CRCData.CalculateCrc16(Send_buf, out Send_buf[7], out Send_buf[8]);
                try
                {
                    MainForm.comPort.Write(Send_buf, 0, 9);
                }
                catch
                {
                    MessageBox.Show("串口有误，请检查", "错误");
                    return;
                }
                T1.Enabled = false;
                T1.Interval = 2000;
                T1.Enabled = true;

                Thread.Sleep(200);
            }
            else if (Page54.Checked == true)
            {
                Send_buf[6] = 0x0e;
                Send_buf[3] = byte.Parse((Convert.ToInt32(RM692CX_Time.Text.Trim(), 16)).ToString());
                Send_buf[4] = 0x03;  //page 选择
                Send_buf[5] = byte.Parse((Convert.ToInt32(Gamma_Nox.Text.Trim(), 16)).ToString());//Gamma x
                CRCData.CalculateCrc16(Send_buf, out Send_buf[7], out Send_buf[8]);
                try
                {
                    MainForm.comPort.Write(Send_buf, 0, 9);
                }
                catch
                {
                    MessageBox.Show("串口有误，请检查", "错误");
                    return;
                }
                T1.Enabled = false;
                T1.Interval = 2000;
                T1.Enabled = true;
                Thread.Sleep(200);
            }
            else if (Page56.Checked == true)
            {
                Send_buf[6] = 0x0f;
                Send_buf[3] = byte.Parse((Convert.ToInt32(RM692CX_Time.Text.Trim(), 16)).ToString());
                Send_buf[4] = 0x04;  //page 选择
                Send_buf[5] = byte.Parse((Convert.ToInt32(Gamma_Nox.Text.Trim(), 16)).ToString());//Gamma x
                CRCData.CalculateCrc16(Send_buf, out Send_buf[7], out Send_buf[8]);
                try
                {
                    MainForm.comPort.Write(Send_buf, 0, 9);
                }
                catch
                {
                    MessageBox.Show("串口有误，请检查", "错误");
                }
                T1.Enabled = false;
                T1.Interval = 2000;
                T1.Enabled = true;

                Thread.Sleep(200);
            }
            else if (RM692CX_Other.Checked == true)
            {
                //otp 40\72\60\70\90\A0\D0\12
                Send_buf[6] = 0x10;
                Send_buf[3] = byte.Parse((Convert.ToInt32(RM692CX_Time.Text.Trim(), 16)).ToString());//OTP次数
                Send_buf[4] = 0x05;  //page 选择
                                     //Send_buf[5] = byte.Parse((Convert.ToInt32(Gamma_Nox.Text.Trim(), 16)).ToString());//Gamma x
                CRCData.CalculateCrc16(Send_buf, out Send_buf[7], out Send_buf[8]);
                try
                {
                    MainForm.comPort.Write(Send_buf, 0, 9);
                }
                catch
                {
                    MessageBox.Show("串口有误，请检查", "错误");
                    return;
                }
                T1.Enabled = false;
                T1.Interval = 2000;
                T1.Enabled = true;
            }
            else if (RM692CX_Demura.Checked == true)
            {
                Send_buf[6] = 0x10;
                Send_buf[3] = byte.Parse((Convert.ToInt32(RM692CX_Time.Text.Trim(), 16)).ToString());//OTP次数
                Send_buf[4] = 0x06;  //page 选择
                                     //Send_buf[5] = byte.Parse((Convert.ToInt32(Gamma_Nox.Text.Trim(), 16)).ToString());//Gamma x
                CRCData.CalculateCrc16(Send_buf, out Send_buf[7], out Send_buf[8]);
                try
                {
                    MainForm.comPort.Write(Send_buf, 0, 9);
                }
                catch
                {
                    MessageBox.Show("串口有误，请检查", "错误");
                    return;
                }
                T1.Enabled = false;
                T1.Interval = 2000;
                T1.Enabled = true;
            }
            else if (RM692CX_RCN.Checked == true)
            {
                Send_buf[6] = 0x10;
                Send_buf[3] = byte.Parse((Convert.ToInt32(RM692CX_Time.Text.Trim(), 16)).ToString());//OTP次数
                Send_buf[4] = 0x07;  //page 选择
                                     //Send_buf[5] = byte.Parse((Convert.ToInt32(Gamma_Nox.Text.Trim(), 16)).ToString());//Gamma x
                CRCData.CalculateCrc16(Send_buf, out Send_buf[7], out Send_buf[8]);
                try
                {
                    MainForm.comPort.Write(Send_buf, 0, 9);
                }
                catch
                {
                    MessageBox.Show("串口有误，请检查", "错误");
                }
                T1.Enabled = false;
                T1.Interval = 2000;
                T1.Enabled = true;
            }
        }
        /*
        private void RM692CX_OTP_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[8];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 8;

            Send_buf[0] = 0x3F;        //RM692CX
            Send_buf[1] = 0x03;
            Send_buf[2] = 0x03;
            Send_buf[3] = byte.Parse((Convert.ToInt32(Gamma_Nox.Text.Trim(), 16)).ToString());
            switch (btnTmp.Name)
            {
                case "OTP_Page50": // otp Page50      A
                    {
                        Send_buf[5] = 0x0A;
                        //Send_buf[3] = 0x00;
                        CRCData.CalculateCrc16(Send_buf, out Send_buf[6], out Send_buf[7]);
                        try
                        {
                            MainForm.comPort.Write(Send_buf, 0,8);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }
                        T1.Enabled = false;
                        T1.Interval = 2000;
                        T1.Enabled = true;

                        //Delay(100);

                        if (radio_Page50.Checked == true)
                        {
                            if (Convert.ToDecimal(RM692CX_Time.Text) > 4)
                            {
                                MessageBox.Show("OTP次数不能大于4次", "警告");
                                return;
                            }
                            else
                            {
                                Send_buf[5] = 0x0A;
                                CRCData.CalculateCrc16(Send_buf, out Send_buf[6], out Send_buf[7]);
                                try
                                {
                                    MainForm.comPort.Write(Send_buf, 0, 8);
                                }
                                catch
                                {
                                    MessageBox.Show("串口有误，请检查", "错误");
                                }
                                T1.Enabled = false;
                                T1.Interval = 2000;
                                T1.Enabled = true;
                            }
                        }

                    }
                    break;
                case "OTP_Page52":// otp Page52        D
                    {
                        Send_buf[5] = 0x0D;
                        //Send_buf[3] = 0x01;
                        CRCData.CalculateCrc16(Send_buf, out Send_buf[6], out Send_buf[7]);
                        try
                        {
                            MainForm.comPort.Write(Send_buf, 0,8);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }
                        T1.Enabled = false;
                        T1.Interval = 2000;
                        T1.Enabled = true;

                        //Delay(100);

                        if (radio_Page50.Checked == true)
                        {
                            if (Convert.ToDecimal(RM692CX_Time.Text) > 4)
                            {
                                MessageBox.Show("OTP次数不能大于4次", "警告");
                                return;
                            }
                            else
                            {
                                Send_buf[5] = 0x0D;

                                CRCData.CalculateCrc16(Send_buf, out Send_buf[6], out Send_buf[7]);
                                try
                                {
                                    MainForm.comPort.Write(Send_buf, 0,8);
                                }
                                catch
                                {
                                    MessageBox.Show("串口有误，请检查", "错误");
                                }
                                T1.Enabled = false;
                                T1.Interval = 2000;
                                T1.Enabled = true;
                            }
                        }
                    }
                    break;
                case "OTP_Page54"://otpPage54        E
                    {
                        Send_buf[5] = 0x0E;
                        //Send_buf[3] = 0x02;
                        CRCData.CalculateCrc16(Send_buf, out Send_buf[6], out Send_buf[7]);
                        try
                        {
                            MainForm.comPort.Write(Send_buf, 0,8);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }
                        T1.Enabled = false;
                        T1.Interval = 2000;
                        T1.Enabled = true;

                        //Delay(100);

                        if (radio_Page54.Checked == true)
                        {
                            if (Convert.ToDecimal(RM692CX_Time.Text) > 3)
                            {
                                MessageBox.Show("OTP次数不能大于3次", "警告");
                                return;
                            }
                            else
                            {
                                Send_buf[5] = 0x0E;

                                CRCData.CalculateCrc16(Send_buf, out Send_buf[6], out Send_buf[7]);
                                try
                                {
                                    MainForm.comPort.Write(Send_buf, 0,8);
                                }
                                catch
                                {
                                    MessageBox.Show("串口有误，请检查", "错误");
                                }
                                T1.Enabled = false;
                                T1.Interval = 2000;
                                T1.Enabled = true;
                            }
                        }
                    }
                    break;
                case "OTP_Page56"://otp Page56                   F
                    {
                        RM350_0XFE_XX = 0x00;
                        Send_buf[5] = 0x0F;
                        //Send_buf[3] = 0x03;
                        CRCData.CalculateCrc16(Send_buf, out Send_buf[6], out Send_buf[7]);
                        try
                        {
                            MainForm.comPort.Write(Send_buf, 0, 8);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }
                        T1.Enabled = false;
                        T1.Interval = 2000;
                        T1.Enabled = true;

                        //Delay(100);

                        if (radio_Page56.Checked == true)
                        {
                            if (Convert.ToDecimal(RM692CX_Time.Text) > 3)
                            {
                                MessageBox.Show("OTP次数不能大于3次", "警告");
                                return;
                            }
                            else
                            {
                                RM350_0XFE_XX = 0x00;
                                Send_buf[5] = 0x0F;

                                CRCData.CalculateCrc16(Send_buf, out Send_buf[6], out Send_buf[7]);
                                try
                                {
                                    MainForm.comPort.Write(Send_buf, 0,8);
                                }
                                catch
                                {
                                    MessageBox.Show("串口有误，请检查", "错误");
                                }
                                T1.Enabled = false;
                                T1.Interval = 2000;
                                T1.Enabled = true;
                            }
                        }
                    }
                    break;
                case "RM692CX_OTP_other":
                    {
                        //otp 40\72\60\70\90\A0\D0\12
                        Send_buf[5] = 0x1C;
                        //Send_buf[3] = 0x04;
                        CRCData.CalculateCrc16(Send_buf, out Send_buf[6], out Send_buf[7]);
                        try
                        {
                            MainForm.comPort.Write(Send_buf, 0,8);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }
                        T1.Enabled = false;
                        T1.Interval = 2000;
                        T1.Enabled = true;
                    }
                    break;
            }
        }
        */


        private void GammaDataRevew(double index, double Dgv_lum, double Dgv_x, double Dgv_y) //---显示亮度数据---
        {
            int Row_Accum = 0, Current_Index = 0, Row_flag = 0;

            DataGridViewRow Row1 = new DataGridViewRow();
            Row_Accum = this.GammaDataView1.Rows.Count - 1;
            if (Row_Accum != 0)
            {
                Current_Index = this.GammaDataView1.SelectedCells[0].RowIndex;
                if ((Current_Index != Row_Accum - 1) && (Current_Index != Row_Accum))
                {
                    Row_Accum = Current_Index;
                    Row_flag = 1;
                }
                else
                {
                    this.GammaDataView1.Rows.Add(Row1);
                }
                if ((Row_Accum != 0) && (Row_flag != 1))
                {
                    if (Current_Index != Row_Accum)
                    {
                        this.GammaDataView1.Rows[Row_Accum - 1].Selected = false;
                        this.GammaDataView1.Rows[Row_Accum].Selected = true;
                    }
                }
            }
            else
            {
                this.GammaDataView1.Rows.Add(Row1);
            }
            this.GammaDataView1.Rows[Row_Accum].Cells[0].Value = index;//序号
            //Rows[x]为第x行,Cells[x]为第x列
            this.GammaDataView1.Rows[Row_Accum].Cells[1].Value = Dgv_x.ToString("#0.0000");
            this.GammaDataView1.Rows[Row_Accum].Cells[2].Value = Dgv_y.ToString("#0.0000");
            this.GammaDataView1.Rows[Row_Accum].Cells[3].Value = Dgv_lum.ToString("#0.0000");
            if (Row_flag == 1)
            {
                Row_flag = 0;
                Row_Accum = this.GammaDataView1.Rows.Count - 1;
                this.GammaDataView1.Rows[Current_Index].Selected = false;
                this.GammaDataView1.Rows[Row_Accum - 1].Selected = true;
            }
            GammaDataView1.FirstDisplayedScrollingRowIndex = GammaDataView1.RowCount - 1;
        }




        #endregion//-----------------RM692CX_END--------
        #region//--------------窗口处理-----------------
        frmMain MainForm = null;

        public RM692Cx_ProjA(frmMain Frm)
        {
            MainForm = Frm;
            InitializeComponent();
            //MainForm.comPort.DataReceived += new SerialDataReceivedEventHandler(SerialPort1_DataReceived);
        }
        private void btnBack_Click(object sender, EventArgs e)
        {   //返回主窗口
            MainForm.comPort.DataReceived -= new SerialDataReceivedEventHandler(SerialPort1_DataReceived);//移除本窗口串口处理函数
            MainForm.comPort.DataReceived += new SerialDataReceivedEventHandler(MainForm.SerialPort1_DataReceived);//
            MainForm.Show();
            this.Hide();
        }

        private void btnCa410Connect_Click(object sender, EventArgs e)
        {

        }

        private void LoadGamma_Click(object sender, EventArgs e)
        {

            OpenFileDialog of = new OpenFileDialog();
            string strtmp = "";
            of.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; ;
            of.Title = "打开Gamma文件";

            if (of.ShowDialog() == DialogResult.OK)
            {
                //读取文本信息
                FileStream fs = new FileStream(of.FileName, FileMode.Open, FileAccess.Read);
                StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
                streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                string strLine = streamReader.ReadLine();
                //循环读取文本信息
                while (!string.IsNullOrEmpty(strLine))
                {
                    strtmp = strtmp + strLine + ",";
                    strLine = streamReader.ReadLine(); //循环读取下一行
                }
                fs.Close(); //关闭流
                streamReader.Close();

                string[] arrtmp = strtmp.Split(new Char[] { ',' });

                //if (MainForm.IC_comboBox.SelectedIndex == 20-1) //RM692CX  DJ_ADD
                {
                    //RM692CX_R0.Text = arrtmp[1].ToString(); RM692CX_G0.Text = arrtmp[3].ToString(); RM692CX_B0.Text = arrtmp[5].ToString();
                    RM692CX_R1.Text = arrtmp[7].ToString(); RM692CX_G1.Text = arrtmp[9].ToString(); RM692CX_B1.Text = arrtmp[11].ToString();
                    RM692CX_R2.Text = arrtmp[13].ToString(); RM692CX_G2.Text = arrtmp[15].ToString(); RM692CX_B2.Text = arrtmp[17].ToString();
                    RM692CX_R3.Text = arrtmp[19].ToString(); RM692CX_G3.Text = arrtmp[21].ToString(); RM692CX_B3.Text = arrtmp[23].ToString();
                    RM692CX_R4.Text = arrtmp[25].ToString(); RM692CX_G4.Text = arrtmp[27].ToString(); RM692CX_B4.Text = arrtmp[29].ToString();
                    RM692CX_R5.Text = arrtmp[31].ToString(); RM692CX_G5.Text = arrtmp[33].ToString(); RM692CX_B5.Text = arrtmp[35].ToString();
                    RM692CX_R6.Text = arrtmp[37].ToString(); RM692CX_G6.Text = arrtmp[39].ToString(); RM692CX_B6.Text = arrtmp[41].ToString();
                    RM692CX_R7.Text = arrtmp[43].ToString(); RM692CX_G7.Text = arrtmp[45].ToString(); RM692CX_B7.Text = arrtmp[47].ToString();
                    RM692CX_R8.Text = arrtmp[49].ToString(); RM692CX_G8.Text = arrtmp[51].ToString(); RM692CX_B8.Text = arrtmp[53].ToString();
                    RM692CX_R9.Text = arrtmp[55].ToString(); RM692CX_G9.Text = arrtmp[57].ToString(); RM692CX_B9.Text = arrtmp[59].ToString();
                    RM692CX_R10.Text = arrtmp[61].ToString(); RM692CX_G10.Text = arrtmp[63].ToString(); RM692CX_B10.Text = arrtmp[65].ToString();
                    RM692CX_R11.Text = arrtmp[67].ToString(); RM692CX_G11.Text = arrtmp[69].ToString(); RM692CX_B11.Text = arrtmp[71].ToString();
                    RM692CX_R12.Text = arrtmp[73].ToString(); RM692CX_G12.Text = arrtmp[75].ToString(); RM692CX_B12.Text = arrtmp[77].ToString();
                    RM692CX_R13.Text = arrtmp[79].ToString(); RM692CX_G13.Text = arrtmp[81].ToString(); RM692CX_B13.Text = arrtmp[83].ToString();
                    RM692CX_R14.Text = arrtmp[85].ToString(); RM692CX_G14.Text = arrtmp[87].ToString(); RM692CX_B14.Text = arrtmp[89].ToString();
                    RM692CX_R15.Text = arrtmp[91].ToString(); RM692CX_G15.Text = arrtmp[93].ToString(); RM692CX_B15.Text = arrtmp[95].ToString();
                    RM692CX_R16.Text = arrtmp[97].ToString(); RM692CX_G16.Text = arrtmp[99].ToString(); RM692CX_B16.Text = arrtmp[101].ToString();
                    RM692CX_R17.Text = arrtmp[103].ToString(); RM692CX_G17.Text = arrtmp[105].ToString(); RM692CX_B17.Text = arrtmp[107].ToString();
                    RM692CX_R18.Text = arrtmp[109].ToString(); RM692CX_G18.Text = arrtmp[111].ToString(); RM692CX_B18.Text = arrtmp[113].ToString();
                    RM692CX_R19.Text = arrtmp[115].ToString(); RM692CX_G19.Text = arrtmp[117].ToString(); RM692CX_B19.Text = arrtmp[119].ToString();
                    RM692CX_R20.Text = arrtmp[121].ToString(); RM692CX_G20.Text = arrtmp[123].ToString(); RM692CX_B20.Text = arrtmp[125].ToString();
                    RM692CX_R21.Text = arrtmp[127].ToString(); RM692CX_G21.Text = arrtmp[129].ToString(); RM692CX_B21.Text = arrtmp[131].ToString();
                    RM692CX_R22.Text = arrtmp[133].ToString(); RM692CX_G22.Text = arrtmp[135].ToString(); RM692CX_B22.Text = arrtmp[137].ToString();
                    RM692CX_R23.Text = arrtmp[139].ToString(); RM692CX_G23.Text = arrtmp[141].ToString(); RM692CX_B23.Text = arrtmp[143].ToString();
                    RM692CX_R24.Text = arrtmp[145].ToString(); RM692CX_G24.Text = arrtmp[147].ToString(); RM692CX_B24.Text = arrtmp[149].ToString();
                    RM692CX_R25.Text = arrtmp[151].ToString(); RM692CX_G25.Text = arrtmp[153].ToString(); RM692CX_B25.Text = arrtmp[155].ToString();
                    RM692CX_R26.Text = arrtmp[157].ToString(); RM692CX_G26.Text = arrtmp[159].ToString(); RM692CX_B26.Text = arrtmp[161].ToString();
                    RM692CX_R27.Text = arrtmp[163].ToString(); RM692CX_G27.Text = arrtmp[165].ToString(); RM692CX_B27.Text = arrtmp[167].ToString();
                    RM692CX_R28.Text = arrtmp[169].ToString(); RM692CX_G28.Text = arrtmp[171].ToString(); RM692CX_B28.Text = arrtmp[173].ToString();
                    RM692CX_R29.Text = arrtmp[175].ToString(); RM692CX_G29.Text = arrtmp[177].ToString(); RM692CX_B29.Text = arrtmp[179].ToString();
                    RM692CX_R30.Text = arrtmp[181].ToString(); RM692CX_G30.Text = arrtmp[183].ToString(); RM692CX_B30.Text = arrtmp[185].ToString();
                    //RM692CX_R31.Text = arrtmp[187].ToString(); RM692CX_G31.Text = arrtmp[189].ToString(); RM692CX_B31.Text = arrtmp[191].ToString();
                    //RM692CX_R32.Text = arrtmp[193].ToString(); RM692CX_G32.Text = arrtmp[195].ToString(); RM692CX_B32.Text = arrtmp[197].ToString();
                }


            }

        }
        void auto_gamma_write(UInt16 addr, UInt16 datar, UInt16 addg, UInt16 datag, UInt16 addb, UInt16 Datab)//一组gamma写入
        {
            Send_buf = new byte[17 + 1];
            Setting.TempRecord.cmdLen = 17;

            if ((datar < 0x000f) || (datag < 0x000f) || (Datab < 0x000f) || (datar > 0x0ff0) || (datag > 0x0ff0) || (Datab > 0x0ff0))
            {
                T5.Enabled = false;

                flag1 = 1;
                flag2 = 1;
                flag3 = 1;
                adjust_Rnum = 0;
                adjust_Gnum = 0;
                adjust_Bnum = 0;
                adjust_l = 0;
                adjust_x = 0;
                adjust_y = 0;
                textBox1.Text += "写入数据检查：数值超出界限，请注意\r\n";
                textBox1.Select(textBox1.Text.Length, 0);
                textBox1.ScrollToCaret();
            }

            Send_buf[0] = 0x3F;//RM692CX
            Send_buf[1] = 0x08;//AUTO Gamma 标志位
            Send_buf[2] = 12 + 1;  //有效数据长度，新增一个供上位机传输额外控制命令：通知下位机是否强切指定PAGE
            Send_buf[3] = Convert.ToByte(addr >> 8);
            Send_buf[4] = Convert.ToByte(datar >> 8);
            Send_buf[5] = Convert.ToByte(addr & 0xff);
            Send_buf[6] = Convert.ToByte(datar & 0xff);
            Send_buf[7] = Convert.ToByte(addg >> 8);
            Send_buf[8] = Convert.ToByte(datag >> 8);
            Send_buf[9] = Convert.ToByte(addg & 0xff);
            Send_buf[10] = Convert.ToByte(datag & 0xff);
            Send_buf[11] = Convert.ToByte(addb >> 8);
            Send_buf[12] = Convert.ToByte(Datab >> 8);
            Send_buf[13] = Convert.ToByte(addb & 0xff);
            Send_buf[14] = Convert.ToByte(Datab & 0xff);
            if (chkNoSetPage.Checked)
                Send_buf[14 + 1] = 0x80; //新增指示下位机是否强切page
            CRCData.CalculateCrc16(Send_buf, out Send_buf[15 + 1], out Send_buf[16 + 1]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 17 + 1);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
                return;
            }
        }
        //btnSaverdGamma
        private void btnSaveRDGamma_Click(object sender, EventArgs e)
        {
            NumericUpDown[] NUMR = new NumericUpDown[] { RM692CX_R30, RM692CX_R29, RM692CX_R28, RM692CX_R27, RM692CX_R26, RM692CX_R25, RM692CX_R24, RM692CX_R23, RM692CX_R22, RM692CX_R21, RM692CX_R20, RM692CX_R19, RM692CX_R18, RM692CX_R17, RM692CX_R16, RM692CX_R15, RM692CX_R14, RM692CX_R13, RM692CX_R12, RM692CX_R11, RM692CX_R10, RM692CX_R9, RM692CX_R8, RM692CX_R7, RM692CX_R6, RM692CX_R5, RM692CX_R4, RM692CX_R3, RM692CX_R2, RM692CX_R1 };
            NumericUpDown[] NUMG = new NumericUpDown[] { RM692CX_G30, RM692CX_G29, RM692CX_G28, RM692CX_G27, RM692CX_G26, RM692CX_G25, RM692CX_G24, RM692CX_G23, RM692CX_G22, RM692CX_G21, RM692CX_G20, RM692CX_G19, RM692CX_G18, RM692CX_G17, RM692CX_G16, RM692CX_G15, RM692CX_G14, RM692CX_G13, RM692CX_G12, RM692CX_G11, RM692CX_G10, RM692CX_G9, RM692CX_G8, RM692CX_G7, RM692CX_G6, RM692CX_G5, RM692CX_G4, RM692CX_G3, RM692CX_G2, RM692CX_G1 };
            NumericUpDown[] NUMB = new NumericUpDown[] { RM692CX_B30, RM692CX_B29, RM692CX_B28, RM692CX_B27, RM692CX_B26, RM692CX_B25, RM692CX_B24, RM692CX_B23, RM692CX_B22, RM692CX_B21, RM692CX_B20, RM692CX_B19, RM692CX_B18, RM692CX_B17, RM692CX_B16, RM692CX_B15, RM692CX_B14, RM692CX_B13, RM692CX_B12, RM692CX_B11, RM692CX_B10, RM692CX_B9, RM692CX_B8, RM692CX_B7, RM692CX_B6, RM692CX_B5, RM692CX_B4, RM692CX_B3, RM692CX_B2, RM692CX_B1 };

            gamma_r = GAMMAs.RM692CX_Gamma_r; gamma_g = GAMMAs.RM692CX_Gamma_g; gamma_b = GAMMAs.RM692CX_Gamma_b;


            SaveFileDialog sf = new SaveFileDialog(); //对话框
            sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义保存的文件的类型
            string str = "保存Gamma文件\r\n";
            if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
            {
                if (sf.FileName == "") return; //如果没有输入文件名
                try
                {
                    StreamWriter sw = null; //文件流
                    sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);
                    //if (MainForm.IC_comboBox.SelectedIndex == 20-1)
                    {
                        //String.Format("{0:X2}", Rec_tmp[i])
                        for (int i = 0; i < 30; i++)
                        {
                            str = str + "[" + String.Format("{0:X2}", gamma_r[30 - i] >> 8) + "]=" + String.Format("{0:X2}", byte.Parse((Convert.ToInt32(NUMR[i].Text, 16) >> 8).ToString())) + "\r\n" + "[" + String.Format("{0:X2}", gamma_r[30 - i] & 0xff) + "]=" + String.Format("{0:X2}", byte.Parse((Convert.ToInt32(NUMR[i].Text, 16) & 0xff).ToString())) + "\r\n" +
                                        "[" + String.Format("{0:X2}", gamma_g[30 - i] >> 8) + "]=" + String.Format("{0:X2}", byte.Parse((Convert.ToInt32(NUMG[i].Text, 16) >> 8).ToString())) + "\r\n" + "[" + String.Format("{0:X2}", gamma_g[30 - i] & 0xff) + "]=" + String.Format("{0:X2}", byte.Parse((Convert.ToInt32(NUMG[i].Text, 16) & 0xff).ToString())) + "\r\n" +
                                        "[" + String.Format("{0:X2}", gamma_b[30 - i] >> 8) + "]=" + String.Format("{0:X2}", byte.Parse((Convert.ToInt32(NUMB[i].Text, 16) >> 8).ToString())) + "\r\n" + "[" + String.Format("{0:X2}", gamma_b[30 - i] & 0xff) + "]=" + String.Format("{0:X2}", byte.Parse((Convert.ToInt32(NUMB[i].Text, 16) & 0xff).ToString())) + "\r\n";
                        }

                        //str = str + "[3D]=" + byte.Parse((Convert.ToInt32(RM692CX_R30.Text, 16) >> 8).ToString()) + "\r\n" + "[3F]=" + byte.Parse((Convert.ToInt32(RM692CX_R30.Text, 16) & 0xff).ToString()) + "\r\n" + "[83]=" + byte.Parse((Convert.ToInt32(RM692CX_G30.Text) >> 8).ToString()) + "\r\n" + "[84]=" + byte.Parse((Convert.ToInt32(RM692CX_G30.Text, 16) & 0xff).ToString()) + "\r\n" + "[C9]=" + byte.Parse((Convert.ToInt32(RM692CX_B30.Text, 16) >> 8).ToString()) + "\r\n" + "[CA]=" + byte.Parse((Convert.ToInt32(RM692CX_B30.Text, 16) & 0xff).ToString()) + "\r\n";

                        sw.WriteLine(str);
                    }
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流
                    MessageBox.Show("Gamma保存OK！", "系统提示：");
                }
                catch
                {
                    MessageBox.Show("Gamma保存时发生错误，请再试！", "系统提示：");
                }
            }
        }

        private void btnSaveGamma_Click(object sender, EventArgs e)
        {
            SaveFileDialog sf = new SaveFileDialog(); //对话框
            sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义保存的文件的类型
            string str = "保存Gamma文件";
            if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
            {
                if (sf.FileName == "") return; //如果没有输入文件名
                try
                {
                    StreamWriter sw = null; //文件流
                    sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);
                    //if (MainForm.IC_comboBox.SelectedIndex == 20-1)
                    {
                        str = str + "保持兼容用R1," + RM692CX_R1.Text + "," + "保持兼容用G1," + RM692CX_G1.Text + "," + "保持兼容用B1," + RM692CX_B1.Text + "\r\n";
                        str = str + "R1," + RM692CX_R1.Text + "," + "G1," + RM692CX_G1.Text + "," + "B1," + RM692CX_B1.Text + "\r\n";
                        str = str + "R2," + RM692CX_R2.Text + "," + "G2," + RM692CX_G2.Text + "," + "B2," + RM692CX_B2.Text + "\r\n";
                        str = str + "R3," + RM692CX_R3.Text + "," + "G3," + RM692CX_G3.Text + "," + "B3," + RM692CX_B3.Text + "\r\n";
                        str = str + "R4," + RM692CX_R4.Text + "," + "G4," + RM692CX_G4.Text + "," + "B4," + RM692CX_B4.Text + "\r\n";
                        str = str + "R5," + RM692CX_R5.Text + "," + "G5," + RM692CX_G5.Text + "," + "B5," + RM692CX_B5.Text + "\r\n";
                        str = str + "R6," + RM692CX_R6.Text + "," + "G6," + RM692CX_G6.Text + "," + "B6," + RM692CX_B6.Text + "\r\n";
                        str = str + "R7," + RM692CX_R7.Text + "," + "G7," + RM692CX_G7.Text + "," + "B7," + RM692CX_B7.Text + "\r\n";
                        str = str + "R8," + RM692CX_R8.Text + "," + "G8," + RM692CX_G8.Text + "," + "B8," + RM692CX_B8.Text + "\r\n";
                        str = str + "R9," + RM692CX_R9.Text + "," + "G9," + RM692CX_G9.Text + "," + "B9," + RM692CX_B9.Text + "\r\n";
                        str = str + "R10," + RM692CX_R10.Text + "," + "G10," + RM692CX_G10.Text + "," + "B10," + RM692CX_B10.Text + "\r\n";
                        str = str + "R11," + RM692CX_R11.Text + "," + "G11," + RM692CX_G11.Text + "," + "B11," + RM692CX_B11.Text + "\r\n";
                        str = str + "R12," + RM692CX_R12.Text + "," + "G12," + RM692CX_G12.Text + "," + "B12," + RM692CX_B12.Text + "\r\n";
                        str = str + "R13," + RM692CX_R13.Text + "," + "G13," + RM692CX_G13.Text + "," + "B13," + RM692CX_B13.Text + "\r\n";
                        str = str + "R14," + RM692CX_R14.Text + "," + "G14," + RM692CX_G14.Text + "," + "B14," + RM692CX_B14.Text + "\r\n";
                        str = str + "R15," + RM692CX_R15.Text + "," + "G15," + RM692CX_G15.Text + "," + "B15," + RM692CX_B15.Text + "\r\n";
                        str = str + "R16," + RM692CX_R16.Text + "," + "G16," + RM692CX_G16.Text + "," + "B16," + RM692CX_B16.Text + "\r\n";
                        str = str + "R17," + RM692CX_R17.Text + "," + "G17," + RM692CX_G17.Text + "," + "B17," + RM692CX_B17.Text + "\r\n";
                        str = str + "R18," + RM692CX_R18.Text + "," + "G18," + RM692CX_G18.Text + "," + "B18," + RM692CX_B18.Text + "\r\n";
                        str = str + "R19," + RM692CX_R19.Text + "," + "G19," + RM692CX_G19.Text + "," + "B19," + RM692CX_B19.Text + "\r\n";
                        str = str + "R20," + RM692CX_R20.Text + "," + "G20," + RM692CX_G20.Text + "," + "B20," + RM692CX_B20.Text + "\r\n";
                        str = str + "R21," + RM692CX_R21.Text + "," + "G21," + RM692CX_G21.Text + "," + "B21," + RM692CX_B21.Text + "\r\n";
                        str = str + "R22," + RM692CX_R22.Text + "," + "G22," + RM692CX_G22.Text + "," + "B22," + RM692CX_B22.Text + "\r\n";
                        str = str + "R23," + RM692CX_R23.Text + "," + "G23," + RM692CX_G23.Text + "," + "B23," + RM692CX_B23.Text + "\r\n";
                        str = str + "R24," + RM692CX_R24.Text + "," + "G24," + RM692CX_G24.Text + "," + "B24," + RM692CX_B24.Text + "\r\n";
                        str = str + "R25," + RM692CX_R25.Text + "," + "G25," + RM692CX_G25.Text + "," + "B25," + RM692CX_B25.Text + "\r\n";
                        str = str + "R26," + RM692CX_R26.Text + "," + "G26," + RM692CX_G26.Text + "," + "B26," + RM692CX_B26.Text + "\r\n";
                        str = str + "R27," + RM692CX_R27.Text + "," + "G27," + RM692CX_G27.Text + "," + "B27," + RM692CX_B27.Text + "\r\n";
                        str = str + "R28," + RM692CX_R28.Text + "," + "G28," + RM692CX_G28.Text + "," + "B28," + RM692CX_B28.Text + "\r\n";
                        str = str + "R29," + RM692CX_R29.Text + "," + "G29," + RM692CX_G29.Text + "," + "B29," + RM692CX_B29.Text + "\r\n";
                        str = str + "R30," + RM692CX_R30.Text + "," + "G30," + RM692CX_G30.Text + "," + "B30," + RM692CX_B30.Text + "\r\n";
                        //str = str + "R31," + RM692CX_R31.Text + "," + "G31," + RM692CX_G31.Text + "," + "B31," + RM692CX_B31.Text + "\r\n";
                        //str = str + "R32," + RM692CX_R32.Text + "," + "G32," + RM692CX_G32.Text + "," + "B32," + RM692CX_B32.Text + "\r\n";
                        sw.WriteLine(str);
                    }
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流
                    MessageBox.Show("Gamma保存OK！", "系统提示：");
                }
                catch
                {
                    MessageBox.Show("Gamma保存时发生错误，请再试！", "系统提示：");
                }
            }
        }

        private void txtTargetLum_TextChanged(object sender, EventArgs e)
        {
            txt2AX_lum.Text = txtTargetLum.Text;
        }

        private void txt2AX_lum_TextChanged(object sender, EventArgs e)
        {
            txtTargetLum.Text = txt2AX_lum.Text;
        }

        private void LoadGamma_MouseEnter(object sender, EventArgs e)
        {
            ToolTip p = new ToolTip();
            p.ShowAlways = true;
            p.SetToolTip(this.LoadGamma, "Load Gamma");

        }

        private void btnAutoGamma_MouseEnter(object sender, EventArgs e)
        {
            ToolTip p = new ToolTip();
            p.ShowAlways = true;
            p.SetToolTip(this.btnAutoGamma, "默认设置，一键Gamma");
        }

        private void btnCal_manual_MouseEnter(object sender, EventArgs e)
        {
            ToolTip p = new ToolTip();
            p.ShowAlways = true;
            p.SetToolTip(this.btnCal_manual, "CA310连接校准，自带黑画面，按下静候6秒");
        }

        private void btnPage0set_MouseEnter(object sender, EventArgs e)
        {
            ToolTip p = new ToolTip();
            p.ShowAlways = true;
            p.SetToolTip(this.btnPage0set, "切入PAGE0");
        }

        private void txtTimeShow_MouseEnter(object sender, EventArgs e)
        {
            ToolTip p = new ToolTip();
            p.ShowAlways = true;
            p.SetToolTip(this.txtTimeShow, "Gamma调整耗时");
        }

        private void btnCleantextbox1_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
        }
        //浏览图片
        string defaultfilePath = "";
        string path = "";
        private void BtnBrowsePic_MouseEnter(object sender, EventArgs e)
        {
            ToolTip p = new ToolTip();
            p.ShowAlways = true;
            p.SetToolTip(this.btnBrowsePic, "右键点此按钮刷新当前目录下图片");
        }
        private void BtnBrowsePic_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (!Directory.Exists(path))
                {
                    MessageBox.Show("图片文件夹不存在.");
                    return;
                }
                DirectoryInfo di = new DirectoryInfo(path);
                this.listBox3_BMPFile.Items.Clear();
                this.CreateListItem(di);//递归加载文件夹里的所有图片
                textBox1.Text += "图片文件夹已刷新\r\n";
            }
        }
        private void btnBrowsePic_Click(object sender, EventArgs e)    //载入画面按键  选择文件夹
        {
            if (textBox2.Text != "")
            {
                defaultfilePath = textBox2.Text;
            }
            else
            {
                StreamReader streamReader = new StreamReader("./update/Picaddr.txt", Encoding.Default);
                defaultfilePath = streamReader.ReadLine();
                streamReader.Close();
            }
            FolderBrowserDialog fldb = new FolderBrowserDialog();
            //首次defaultfilePath为空，按FolderBrowserDialog默认设置（即桌面）选择
            if (defaultfilePath != "")
            {                //设置此次默认目录为上一次选中目录
                fldb.SelectedPath = defaultfilePath;
            }
            fldb.ShowNewFolderButton = false;
            DialogResult res = fldb.ShowDialog();
            if (res != System.Windows.Forms.DialogResult.OK)
            {
                MessageBox.Show("没选择任何文件夹.");
                return;
            }
            defaultfilePath = fldb.SelectedPath;//记录选中的目录
            path = fldb.SelectedPath;
            if (!Directory.Exists(path))
            {
                MessageBox.Show("文件夹不存在.");
                return;
            }
            DirectoryInfo di = new DirectoryInfo(path);
            this.listBox3_BMPFile.Items.Clear();
            this.CreateListItem(di);//递归加载文件夹里的所有图片
            textBox2.Text = path;
            // -------------------------------------------------------------------------
            StreamWriter sw = null; //文件流
            sw = new StreamWriter("./update/Picaddr.txt", false, Encoding.Default);//Encoding.GetEncoding("UTF-8")
            sw.Write(defaultfilePath);
            sw.Dispose(); //释放资源
            sw.Close(); //关闭数据流

        }
        /// <summary>
        /// 递归文件夹，把文件夹里所有后缀类似图片的文件，加入listbox
        /// </summary>
        /// <param name="di"></param>
        private void CreateListItem(DirectoryInfo di)
        {
            foreach (DirectoryInfo childDi in di.GetDirectories())
            {
                this.CreateListItem(childDi);
            }
            foreach (FileInfo fi in di.GetFiles())
            {
                if (ImageType.Contains(fi.Extension))//找到一个图片(如果用户手贱把一个excel文件后缀改成jpeg没办法)
                {
                    this.listBox3_BMPFile.Items.Add(new MyImage(fi.FullName));
                }
            }
        }
        private void lstImage_Click(object sender, EventArgs e)
        {
            if (listBox3_BMPFile.SelectedItem == null)
            {
                return;
            }
            MyImage selectedImg = (MyImage)listBox3_BMPFile.SelectedItem;
            //Image img = new Bitmap(selectedImg.Path);
            //pictureBox1.Image = img;
            FileStream fileStream = new FileStream(selectedImg.Path, FileMode.Open, FileAccess.Read);
            pictureBox1.Image = Image.FromStream(fileStream);
            fileStream.Close();
            fileStream.Dispose();
        }
        System.DateTime TimeNow2 = new DateTime();
        TimeSpan TimeCount2 = new TimeSpan();
        private void btnPrevPic_Click(object sender, EventArgs e)
        {
            MemoryStream aFile;
            aFile = new MemoryStream();
            //FileStream aFile;
            Int32 m_txlen = 0;

            int HACT_True;
            int VACT_True;
            int j;
            TimeNow2 = DateTime.Now; //开始计时

            byte[] TemFileContent = new byte[512 * 4000 + 100];
            int ByteReaded = 0x00;

            string FileName = textBox2.Text + '\\' + listBox3_BMPFile.SelectedItem.ToString();

            Bitmap bmp = new Bitmap(FileName);
            bmp.RotateFlip(RotateFlipType.Rotate180FlipX); //Rotate180FlipX，旋转180度        
            bmp.Save(aFile, System.Drawing.Imaging.ImageFormat.Bmp);
            aFile.Seek(0, SeekOrigin.Begin);//           
            ByteReaded = aFile.Read(TemFileContent, 0, 54);

            HACT_True = ((TemFileContent[19]) << 8) + TemFileContent[18];
            VACT_True = ((TemFileContent[23] << 8) + TemFileContent[22]);
            if (MainForm.HACT.Value != HACT_True)
            {
                textBox1.Text += "图片宽度与设定不符\r\n";
                return;
            }

            if ((MainForm.Dis_mode.SelectedIndex == 1)) //(Dis_mode.SelectedIndex == 1):MIPI 模式，因为下位机仅对MIPI模式做了优化
            {
                //------------20180903---------------
                try
                {   //bd 5a a5 9DF0-----提前下个标志位告诉下位机开始送图了，避免特殊图片格式导致错误配置
                    //BD A5 5A 9DF0-----提前下个标志位告诉下位机开始 快速 送图了
                    TemFileContent[0] = 0xbd; TemFileContent[1] = 0xa5; TemFileContent[2] = 0x5a; TemFileContent[3] = 0x9d; TemFileContent[4] = 0xf0;
                    MainForm.comPort.Write(TemFileContent, 0, 5);
                }
                catch
                {
                    MessageBox.Show("串口有误，请检查", "错误");
                    return;
                }
                //-----------------------------------
                try
                {

                    j = 0;
                    int x, y, s = 0;
                    x = 512 * 4000; //单次传输数量
                    y = (VACT_True * HACT_True * 3 / x); //传输次数
                    if ((VACT_True * HACT_True * 3 % x) != 0)
                        y++;
                    {
                        //aFile.Seek(54, SeekOrigin.Begin);
                        while (j < y)
                        {
                            //if (HACT_True % 4 != 0)
                            //    aFile.Seek((VACT_True - j) * (HACT_True * 3 + (HACT_True % 4)) + 54, SeekOrigin.Begin);
                            //else
                            //    aFile.Seek((VACT_True - j) * HACT_True * 3 + 54, SeekOrigin.Begin);
                            aFile.Seek((j) * x + 54, SeekOrigin.Begin);

                            ByteReaded = aFile.Read(TemFileContent, 0, x);

                            try
                            {
                                MainForm.comPort.Write(TemFileContent, 0, ByteReaded);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                                break;
                            }

                            progressBar1.Value = Convert.ToInt32((double)m_txlen / aFile.Length * 100);
                            label14.Text = progressBar1.Value.ToString() + "%";
                            Application.DoEvents();
                            j++;
                        }

                        TemFileContent[0] = 0x5A;
                        try
                        {
                            MainForm.comPort.Write(TemFileContent, 0, 1);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }

                        aFile.Close();
                    }
                }
                catch
                {
                    MessageBox.Show("请选择文件夹", "错误");
                }
            }
            else
            {
                //------------20180808---------------
                try
                { //5a a5 9DF0731A-----提前下个标志位告诉下位机开始送图了，避免特殊图片格式导致错误配置
                    TemFileContent[0] = 0xbd; TemFileContent[1] = 0x5a; TemFileContent[2] = 0xa5; TemFileContent[3] = 0x9d; TemFileContent[4] = 0xf0;
                    MainForm.comPort.Write(TemFileContent, 0, 5);
                }
                catch
                {
                    MessageBox.Show("串口有误，请检查", "错误");
                    return;
                }
                //-----------------------------------
                try
                {
                    j = 1;
                    //if ((HACT_True == ((TemFileContent[19] << 8) + (TemFileContent[18]))) && (Convert.ToInt32(VACT.Value) == (TemFileContent[23] << 8) + (TemFileContent[22])))//BMP图片水平大小
                    {
                        aFile.Seek(54, SeekOrigin.Begin);
                        while (j <= VACT_True)
                        {
                            if (HACT_True % 4 != 0)
                                aFile.Seek((VACT_True - j) * (HACT_True * 3 + (HACT_True % 4)) + 54, SeekOrigin.Begin);
                            else
                                aFile.Seek((VACT_True - j) * HACT_True * 3 + 54, SeekOrigin.Begin);


                            ByteReaded = aFile.Read(TemFileContent, 0, HACT_True * 3); // 

                            try
                            {
                                MainForm.comPort.Write(TemFileContent, 0, ByteReaded);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                                break;
                            }
                            m_txlen += ByteReaded;
                            progressBar1.Value = Convert.ToInt32((double)m_txlen / aFile.Length * 100);
                            label14.Text = progressBar1.Value.ToString() + "%";
                            Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                            j++;
                        }

                        TemFileContent[0] = 0x5A;
                        try
                        {
                            MainForm.comPort.Write(TemFileContent, 0, 1);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                            return;
                        }

                        aFile.Close();
                    }
                    /*
                    else
                    {
                        MessageBox.Show("BMP分辨率非法");
                    }*/
                }
                catch
                {
                    textBox1.Text += "啥图片都没显示";
                    textBox1.Select(textBox1.Text.Length, 0);
                    textBox1.ScrollToCaret();
                }
            }


            TimeCount2 = DateTime.Now - TimeNow2;
            textBox1.Text = string.Format("{0:00}:{1:00}", TimeCount2.Seconds, TimeCount2.Milliseconds);
        }
        public class MyImage
        {
            public MyImage(string p)
            {
                this.Path = p;
            }
            public string Path { get; set; }
            public override string ToString()
            {
                if (Path == null)
                {
                    return null;
                }
                return System.IO.Path.GetFileName(this.Path);
            }
        }

        private void RM692CX_Load(object sender, EventArgs e)
        {
            string[] s = { ".bmp" };
            this.ImageType = new List<string>(s);
        }

        private void btnReInit_Click(object sender, EventArgs e)
        {
            MainForm.btnRun_Click(MainForm.btnRun, e);
        }

        //private void KPQ_Demura_Help_Click(object sender, EventArgs e)
        //{
        //    MessageBoxTimeoutA((IntPtr )0,"EN-->擦除-->进入-->刷填", "2K平台Demura烧录流程",32,0,19000);            
        //}

        public string[] DDICInitCmdarray;   //DDICInitCmdarray保存脱水后的DriverCode
        private void txtProcessSourcecode(string originDriverCode, out string txtbaseClearOriginal, out string txtMCleatOriginal)
        {
            string filter = "\r\n";
            string TxtSourceCode = originDriverCode;
            TxtSourceCode = TxtSourceCode.Replace("-", ",");
            TxtSourceCode = TxtSourceCode.Replace(" ", "");
            TxtSourceCode = TxtSourceCode.Replace("=", ",");
            TxtSourceCode = TxtSourceCode.Replace(".", ",");
            TxtSourceCode = TxtSourceCode.Replace("DELAY", "delay");//转换为小写，防止被去除
            TxtSourceCode = TxtSourceCode.Replace("0x", "");
            TxtSourceCode = TxtSourceCode.Trim();
            TxtSourceCode = TxtSourceCode.Replace(filter, " ");

            string[] arr1 = TxtSourceCode.Trim().Split(' ');
            string[] arr2 = TxtSourceCode.Trim().Split(' ');
            for (int i = 0; i < arr1.Length; i++)
            {
                int tep = arr1[i].IndexOf("/");
                switch (tep)
                {
                    case 0:
                        {
                            //首字母为‘/’，arr1则保留,为清洁版使用，去除特殊字符，保留注释
                            arr2[i] = "";  //arr2为脱水版使用，只保留有效代码，将特殊字符与注释全部去除
                        }
                        break;
                    case (-1):
                        {
                            //不含有‘/’
                            //去除字符串中的特殊字符
                            arr1[i] = Regex.Replace(arr1[i], "[^0-9A-Fdelay,.;/-=\r\n]", "");
                            arr1[i] = arr1[i].Trim().TrimEnd(",./:=".ToCharArray());
                            arr2[i] = Regex.Replace(arr2[i], "[^0-9A-Fdelay,.;/-=\r\n]", "");
                            arr2[i] = arr2[i].Trim().TrimEnd(",./:=".ToCharArray());
                        }
                        break;
                    default:
                        {
                            arr2[i] = arr2[i].Substring(0, tep);
                            arr2[i] = Regex.Replace(arr2[i], "[^0-9A-Fdelay,.;/-=\r\n]", "");
                        }
                        break;

                }
            }
            string abc1 = string.Join("\r\n", arr1);
            string abc2 = string.Join("\r\n", arr2);
            txtbaseClearOriginal = abc1;
            txtMCleatOriginal = abc2;

        }

        private void Send_Cmd_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            switch (btn.Name)
            {
                case "btnSend1":
                    {
                        MainForm.txtProcessDrivercode(txtCmd1.Text, out string txtbaseClearOriginal, out string txtMCleatOriginal);
                    }
                    break;
                case "btnSend2":
                    {
                        MainForm.txtProcessDrivercode(txtCmd2.Text, out string txtbaseClearOriginal, out string txtMCleatOriginal);
                    }
                    break;
                case "btnSend3":
                    {
                        MainForm.txtProcessDrivercode(txtCmd3.Text, out string txtbaseClearOriginal, out string txtMCleatOriginal);
                    }
                    break;
                case "btnSend4":
                    {
                        MainForm.txtProcessDrivercode(txtCmd4.Text, out string txtbaseClearOriginal, out string txtMCleatOriginal);
                    }
                    break;

            }
            Setting.TempRecord.cmdLen = 6;
            if (Write_LP.Checked)
            {
                MainForm.SendCmd(0);
            }
            else
            {
                MainForm.SendCmd(4);
            }
            textBox1.Text += "命令已下发...\r\n";
            textBox1.Select(textBox1.Text.Length, 0);
            textBox1.ScrollToCaret();
        }

        private void OpenTxtCode_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;

            OpenFileDialog of = new OpenFileDialog();
            of.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            of.Title = "初始化 Code";

            if (of.ShowDialog() == DialogResult.OK)
            {
                StreamReader streamReader = new StreamReader(of.FileName, Encoding.Default);
                switch (btn.Name)
                {
                    case "OpenTxtCode":
                        {
                            txtCmd1.Clear();
                            txtCmd1.Text = streamReader.ReadToEnd();
                        }
                        break;
                    case "OpenTxtCode2":
                        {
                            txtCmd2.Clear();
                            txtCmd2.Text = streamReader.ReadToEnd();
                        }
                        break;
                    case "OpenTxtCode3":
                        {
                            txtCmd3.Clear();
                            txtCmd3.Text = streamReader.ReadToEnd();
                        }
                        break;
                    case "OpenTxtCode4":
                        {
                            txtCmd4.Clear();
                            txtCmd4.Text = streamReader.ReadToEnd();
                        }
                        break;
                    default: break;
                }
                streamReader.Close();
            }
        }

        private void btnSaveDriverCode_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            //sfd.InitialDirectory = @"C:\Users\SpringRain\Desktop";
            sfd.Title = "请选择要保存的文件路径";
            sfd.Filter = "文本文件|*.txt|所有文件|*.*";
            sfd.ShowDialog();

            //获得用户要保存的文件的路径
            string path = sfd.FileName;
            if (path == "")
            {
                return;
            }
            using (FileStream fsWrite = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
            {
                byte[] buffer = Encoding.Default.GetBytes(txtCmd1.Text);
                fsWrite.Write(buffer, 0, buffer.Length);
            }
            MessageBox.Show("保存成功");
        }
        private void SaveCode_Click(object sender, EventArgs e)
        {
            SaveFileDialog txt = new SaveFileDialog();
            txt.Filter = @"txt文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            string str = "//-----保存Driver IC Code文件------\r\n";
            if (txt.ShowDialog() == DialogResult.OK)
            {
                if (txt.FileName == " ")
                {
                    return;
                }
                StreamWriter sw = null; //文件流
                sw = new StreamWriter(txt.FileName, false, Encoding.Default);//Encoding.GetEncoding("UTF-8")
                try
                {
                    if (ChkCMD1.Checked)
                    {
                        str += "//---CMD1---\r\n" + txtCmd1.Text;// lstMIPIDriver.Items[i].ToString();
                        str += "\r\n\r\n";
                    }
                    if (ChkCMD2.Checked)
                    {
                        str += "//---CMD2---\r\n" + txtCmd2.Text;// lstMIPIDriver.Items[i].ToString();
                        str += "\r\n\r\n";
                    }
                    if (ChkCMD3.Checked)
                    {
                        str += "//---CMD3---\r\n" + txtCmd3.Text;// lstMIPIDriver.Items[i].ToString();
                        str += "\r\n\r\n";
                    }
                    if (ChkCMD4.Checked)
                    {
                        str += "//---CMD4---\r\n" + txtCmd4.Text;// lstMIPIDriver.Items[i].ToString();
                        str += "\r\n\r\n";
                    }

                    sw.Write(str);
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流
                    MessageBox.Show("Driver IC Code保存OK！", "系统提示：");
                }
                catch
                {
                    MessageBox.Show("Driver IC Code保存时发生错误，请再试！", "系统提示：");
                }
            }

        }


        private void btn_Pic_Load_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fldb = new FolderBrowserDialog();
            fldb.ShowNewFolderButton = false;
            DialogResult res = fldb.ShowDialog();
            if (res != System.Windows.Forms.DialogResult.OK)
            {
                MessageBox.Show("没选择任何文件夹.");
                return;
            }
            string path = fldb.SelectedPath;
            if (!Directory.Exists(path))
            {
                MessageBox.Show("文件夹不存在.");
                return;
            }
            DirectoryInfo da = new DirectoryInfo(path);
            //this.listBox3_BMPFile.Items.Clear();
            this.CreateListItem(da);//递归加载文件夹里的所有图片
            Pic_Box.Text = path;
            vesa_bmp_path = path;
        }

        private void BtnPshow_Click(object sender, EventArgs e)
        {
            Byte r = 0, g = 0, b = 0;
            r = byte.Parse(numPatternR.Value.ToString());

            g = byte.Parse(numPatternG.Value.ToString());

            b = byte.Parse(numPatternB.Value.ToString());
            picColorshow.BackColor = System.Drawing.Color.FromArgb(r, g, b);
        }

        private void RM692Cx_M2_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible == true) //子窗体已开启标识
            {
                if (MainForm.comPort.IsOpen)
                {
                    MainForm.comPort.DataReceived -= new SerialDataReceivedEventHandler(MainForm.SerialPort1_DataReceived);
                    Thread.Sleep(5);
                    MainForm.comPort.DataReceived += new SerialDataReceivedEventHandler(SerialPort1_DataReceived);
                    Thread.Sleep(5);
                    textBox1.Text += "com 已经连接！\r\n";
                }
            }
            else
            {
                //MainForm.comPort.DataReceived -= new SerialDataReceivedEventHandler(SerialPort1_DataReceived);

            }
        }

        private void Frm_Activated(object sender, EventArgs e)
        {
            if (MainForm.btnGrayShow.Enabled)
            {
                btnGrayShow.Enabled = true;
            }
        }
        private void RM692CX_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            btnBack_Click(btnBack, e);
        }
        #endregion



        //GIR & LIR
        //GIR
        //实时显示310控件显示界面
        private void Msr_CA310_Vis()
        {
            MainForm.Msr_CASDK();
            auto_lum = MainForm.CASDK_LUM;
            auto_x = MainForm.CASDK_x;
            auto_y = MainForm.CASDK_y;
            RM692CX_Lum.Text = Convert.ToString(auto_lum);
            RM692CX_X.Text = Convert.ToString(auto_x);
            RM692CX_Y.Text = Convert.ToString(auto_y);
        }
        private void RM692CX_M2_IR_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[7];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 7;

            Send_buf[0] = 0x3F;        //RM692CX
            Send_buf[2] = 0x02;
            switch (btnTmp.Name)
            {
                //pengcs
                case "btn_GIR_Start":
                    { Send_buf[1] = 0x0C; }//与下位机同时改 GIR_Drop_Start
                    break;
                case "btn_GIR_Stop":
                    { Send_buf[1] = 0x0D; }//与下位机同时改 GIR_Drop_Stop
                    break;
                case "btn_LIR_Start":
                    { Send_buf[1] = 0x10; }//与下位机同时改
                    break;
                case "btn_LIR_Stop":
                    { Send_buf[1] = 0x11; }//与下位机同时改
                    break;
                default:
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 7);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            T1.Enabled = false;
            T1.Interval = 2000;
            T1.Enabled = true;
        }
        private void btn_GIR_adj_Click(object sender, EventArgs e)
        {
            int flag = 1;
            double Lr_d, Lg_d, Lb_d;
            D_lums = new double[33, 3];
            D_lum = new double[33];
            //index = 0;
            index = 2;//pengcs
            adjust_Rnum = 0;
            adjust_Gnum = 0;
            adjust_Bnum = 0;
            adjust_l = 0;
            adjust_x = 0;
            adjust_y = 0;
            flag1 = flag2 = flag3 = 1; GammaAdjustTimes = 0;
            k = 0;
            Button btnTmp = (Button)sender;
            //RM692CX_M2_IR_Click(btn_GIR_Stop, e);

            D_lum[2] = Convert.ToDouble(RM692CX_P30.Text);

            D_x = Convert.ToDouble(txt2AX_X.Text);
            D_y = Convert.ToDouble(txt2AX_Y.Text);
            Lr_d = R_y * ((D_y - G_y) * (B_x - D_x) + (D_x - G_x) * (D_y - B_y)) / B_y / ((D_y - G_y) * (D_x - R_x) + (D_x - G_x) * (R_y - D_y));
            Lb_d = 1.0;
            Lg_d = G_y * ((D_y - R_y) * (B_x - D_x) + (D_x - R_x) * (D_y - B_y)) / B_y / ((D_y - R_y) * (D_x - G_x) + (D_x - R_x) * (G_y - D_y));

            //D_lums[0, 0] = D_lum[2] * Lr_d / (Lr_d + Lg_d + Lb_d);
            //D_lums[0, 1] = D_lum[2] * Lg_d / (Lr_d + Lg_d + Lb_d);
            //D_lums[0, 2] = D_lum[2] * Lb_d / (Lr_d + Lg_d + Lb_d);
            D_lums[index, 0] = D_lum[2] * Lr_d / (Lr_d + Lg_d + Lb_d);
            D_lums[index, 1] = D_lum[2] * Lg_d / (Lr_d + Lg_d + Lb_d);
            D_lums[index, 2] = D_lum[2] * Lb_d / (Lr_d + Lg_d + Lb_d);
            gamma_r = GAMMAs.RM692AX_Gamma_r;
            gamma_g = GAMMAs.RM692AX_Gamma_g;
            gamma_b = GAMMAs.RM692AX_Gamma_b;
            switch (flag)
            {
                case 1://寄存器值与亮度正比
                    {
                        lum_off = Convert.ToDouble(txt2AX_lumoff.Text);
                        x_off = Convert.ToDouble(txt2AX_Xoff.Text);
                        y_off = Convert.ToDouble(txt2AX_Yoff.Text);
                        R66451_btnPrevPic_Run("W255" + ".bmp");
                        T20.Enabled = true;
                        //T19.Enabled = true;
                    }
                    break;
                case 2://寄存器值与亮度反比
                    break;
                default:
                    break;
            }

        }

        private void Num51bit_ValueChanged(object sender, EventArgs e)
        {
            RM692CXHsb_51.Maximum = Convert.ToUInt16(Math.Pow(2, Convert.ToUInt16(num51bit.Value)));
            RM692CXHsb_51.Value = Convert.ToUInt16(Math.Pow(2, Convert.ToUInt16(num51bit.Value))) - 1;
            R692AX_51Reg.Text = RM692CXHsb_51.Value.ToString() + "阶";
        }

        private void T19_Tick(object sender, EventArgs e)
        {
            double Lr_t, Lg_t, Lb_t, lumr, lumg, lumb;
            double lum_ALL, lum_A, lum_Target;
            int num = 1;//调到第二次 跳出停止
            if (num == 1)
            {
                Msr_CA310_Vis();
                if ((auto_lum < (D_lum[index] + lum_off)) && (auto_lum > (D_lum[index] - lum_off)))//G
                    flag1 = 0;
                if ((auto_y < (D_y + y_off)) && (auto_y > (D_y - y_off)) && flag1 == 0)//B
                    flag2 = 0;
                if ((auto_x < (D_x + x_off)) && (auto_x > (D_x - x_off)) && flag1 == 0 && flag2 == 0)//R
                    flag3 = 0;
                if (flag1 == 0 && flag2 == 0 && flag3 == 0)
                {
                    T19.Enabled = false;
                    num++;
                    T20.Enabled = true;


                }
                else
                {   //如果不在规格内则继续调整
                    decimal r = 0, g = 0, b = 0;
                    Lr_t = R_y * ((auto_y - G_y) * (B_x - auto_x) + (auto_x - G_x) * (auto_y - B_y)) / B_y / ((auto_y - G_y) * (auto_x - R_x) + (auto_x - G_x) * (R_y - auto_y));
                    Lb_t = 1.0;
                    Lg_t = G_y * ((auto_y - R_y) * (B_x - auto_x) + (auto_x - R_x) * (auto_y - B_y)) / B_y / ((auto_y - R_y) * (auto_x - G_x) + (auto_x - R_x) * (G_y - auto_y));
                    lumr = auto_lum * Lr_t / (Lr_t + Lg_t + Lb_t);
                    lumg = auto_lum * Lg_t / (Lr_t + Lg_t + Lb_t);
                    lumb = auto_lum * Lb_t / (Lr_t + Lg_t + Lb_t);
                    switch (index)
                    {
                        //case 0:
                        case 2:  //pengcs
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        RM692CX_R30.Value = RM692CX_R30.Value - step_h;
                                    else RM692CX_R30.Value = RM692CX_R30.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;

                                    //pengcs
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }


                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        RM692CX_R30.Value = RM692CX_R30.Value - step_l;
                                    else RM692CX_R30.Value = RM692CX_R30.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        RM692CX_G30.Value = RM692CX_G30.Value - step_h;
                                    else RM692CX_G30.Value = RM692CX_G30.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;

                                    //pengcs                                
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        RM692CX_G30.Value = RM692CX_G30.Value - step_l;
                                    else RM692CX_G30.Value = RM692CX_G30.Value + step_l;
                                }
                                adjust_Gnum++;

                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        RM692CX_B30.Value = RM692CX_B30.Value - step_h;
                                    else RM692CX_B30.Value = RM692CX_B30.Value + step_h;
                                    adjust_Bnum = 0;
                                    //pengcs
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }


                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        RM692CX_B30.Value = RM692CX_B30.Value - step_l;
                                    else RM692CX_B30.Value = RM692CX_B30.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            auto_gamma_write(gamma_r[30], Convert.ToUInt16(RM692CX_R30.Value), gamma_g[30], Convert.ToUInt16(RM692CX_G30.Value), gamma_b[30], Convert.ToUInt16(RM692CX_B30.Value));
                            break;
                    }
                }






            }
            else  //num==2
            {
                Msr_CA310_Vis();
                if ((auto_lum < (D_lum[index] + lum_off)) && (auto_lum > (D_lum[index] - lum_off)))//G
                    flag1 = 0;
                if ((auto_y < (D_y + y_off)) && (auto_y > (D_y - y_off)) && flag1 == 0)//B
                    flag2 = 0;
                if ((auto_x < (D_x + x_off)) && (auto_x > (D_x - x_off)) && flag1 == 0 && flag2 == 0)//R
                    flag3 = 0;
                if (flag1 == 0 && flag2 == 0 && flag3 == 0)
                {
                    Msr_CA310_Vis();
                    lum_ALL = auto_lum;//第二次的目标亮度
                    OLED_Show_Click(btnwhite, e);

                    int j;
                    //string high, low;
                    // 读取j的值
                    RM692CX_Reg_Data.Text = "FE,36";
                    RM692CX_Others_Click(RM692CX_RegW, e);
                    RM692CX_Reg_Data.Text = "A9";
                    RM692CX_Others_Click(RM692CX_RegR, e);
                    RM692CX_Reg_Data.Text.Replace("A9", "0x");
                    j = Convert.ToInt32("RM692CX_Reg_Data.Text", 16); //Convert.ToInt32("0xaa", 16)
                    //高位暂时不管
                    bool i = true;
                    while (i)
                    {
                        Msr_CA310_Vis();
                        if (System.Math.Abs(auto_lum - lum_ALL) <= 5)
                        {
                            Write_GIR_Drop(Convert.ToUInt16(0xA9AA), Convert.ToUInt16(j));
                            T20.Enabled = false;
                            T19.Enabled = false;
                            i = false;
                            MessageBox.Show("GIR_Drop调整完成！");
                        }
                        else if (auto_lum < lum_ALL - 5)
                        {
                            j = j + 1;
                            Write_GIR_Drop(Convert.ToUInt16(0xA9AA), Convert.ToUInt16(j));
                            Msr_CA310_Vis();
                            i = true;
                        }
                        else//auto_lum > lum_A +5
                        {
                            j = j - 1;
                            Write_GIR_Drop(Convert.ToUInt16(0xA9AA), Convert.ToUInt16(j));  //new
                            Msr_CA310_Vis();
                            i = true;
                        }
                    }

                }
                else
                {   //如果不在规格内则继续调整
                    decimal r = 0, g = 0, b = 0;
                    Lr_t = R_y * ((auto_y - G_y) * (B_x - auto_x) + (auto_x - G_x) * (auto_y - B_y)) / B_y / ((auto_y - G_y) * (auto_x - R_x) + (auto_x - G_x) * (R_y - auto_y));
                    Lb_t = 1.0;
                    Lg_t = G_y * ((auto_y - R_y) * (B_x - auto_x) + (auto_x - R_x) * (auto_y - B_y)) / B_y / ((auto_y - R_y) * (auto_x - G_x) + (auto_x - R_x) * (G_y - auto_y));
                    lumr = auto_lum * Lr_t / (Lr_t + Lg_t + Lb_t);
                    lumg = auto_lum * Lg_t / (Lr_t + Lg_t + Lb_t);
                    lumb = auto_lum * Lb_t / (Lr_t + Lg_t + Lb_t);
                    switch (index)
                    {
                        //case 0:
                        case 2:  //pengcs
                            if (flag3 == 1)
                            {
                                if (adjust_Rnum > 10)
                                {
                                    if (lumr > D_lums[index, 0])
                                        RM692CX_R30.Value = RM692CX_R30.Value - step_h;
                                    else RM692CX_R30.Value = RM692CX_R30.Value + step_h;
                                    adjust_Rnum = 0;
                                    adjust_x++;

                                    //pengcs
                                    if (adjust_x > 50)
                                    {
                                        x_off += 0.0005;
                                        adjust_x = 0;
                                    }


                                }
                                else
                                {
                                    if (lumr > D_lums[index, 0])
                                        RM692CX_R30.Value = RM692CX_R30.Value - step_l;
                                    else RM692CX_R30.Value = RM692CX_R30.Value + step_l;
                                }
                                adjust_Rnum++;
                            }
                            if (flag1 == 1)
                            {
                                if (adjust_Gnum > 10)
                                {
                                    if (lumg > D_lums[index, 1])
                                        RM692CX_G30.Value = RM692CX_G30.Value - step_h;
                                    else RM692CX_G30.Value = RM692CX_G30.Value + step_h;
                                    adjust_Gnum = 0;
                                    adjust_l++;

                                    //pengcs                                
                                    if (adjust_l > 50)
                                    {
                                        lum_off *= 1.5;
                                        adjust_l = 0;
                                    }
                                }
                                else
                                {
                                    if (lumg > D_lums[index, 1])
                                        RM692CX_G30.Value = RM692CX_G30.Value - step_l;
                                    else RM692CX_G30.Value = RM692CX_G30.Value + step_l;
                                }
                                adjust_Gnum++;

                            }
                            if (flag2 == 1)
                            {
                                if (adjust_Bnum > 10)
                                {
                                    if (lumb > D_lums[index, 2])
                                        RM692CX_B30.Value = RM692CX_B30.Value - step_h;
                                    else RM692CX_B30.Value = RM692CX_B30.Value + step_h;
                                    adjust_Bnum = 0;
                                    //pengcs
                                    adjust_y++;
                                    if (adjust_y > 50)
                                    {
                                        y_off += 0.0005;
                                        adjust_y = 0;
                                    }


                                }
                                else
                                {
                                    if (lumb > D_lums[index, 2])
                                        RM692CX_B30.Value = RM692CX_B30.Value - step_l;
                                    else RM692CX_B30.Value = RM692CX_B30.Value + step_l;
                                }
                                adjust_Bnum++;
                            }
                            auto_gamma_write(gamma_r[30], Convert.ToUInt16(RM692CX_R30.Value), gamma_g[30], Convert.ToUInt16(RM692CX_G30.Value), gamma_b[30], Convert.ToUInt16(RM692CX_B30.Value));
                            break;
                    }
                }



            }
        }
        private void T20_Tick(object sender, EventArgs e)
        {
            double Lr_t, Lg_t, Lb_t, lumr, lumg, lumb;
            double lum_A;
            Msr_CA310_Vis(); //Thread.Sleep(500); Msr_CA310();
                             //获取当前亮度值并确定其是否在规格范围内   
            lum_A = auto_lum;   //将调好的亮度变成目标亮度


            //送一张白图
            OLED_Show_Click(btnwhite, e);//pengcs
            double j = 172;//AC 写一个j值送一个参考值
            Write_GIR_Drop(Convert.ToUInt16(0xA9AA), Convert.ToUInt16(j));
            Msr_CA310_Vis();

            bool i = true;
            while (i)
            {
                Msr_CA310_Vis();
                if (System.Math.Abs(auto_lum - lum_A) <= 5)
                {
                    Write_GIR_Drop(Convert.ToUInt16(0xA9AA), Convert.ToUInt16(j));
                    Msr_CA310_Vis();
                    T20.Enabled = false;
                    i = false;
                    R66451_btnPrevPic_Run("W255.bmp");//送一张黑白框图
                    T19.Enabled = true;

                }
                else if (auto_lum < lum_A - 5)
                {
                    j = j + 1;
                    // Write_GIR_Drop(Convert.ToUInt16(0xADAF), Convert.ToUInt16(j)); //old
                    Write_GIR_Drop(Convert.ToUInt16(0xA9AA), Convert.ToUInt16(j));  //new
                    i = true;
                    Msr_CA310_Vis();
                }
                else//auto_lum > lum_A +5
                {
                    j = j - 1;

                    Write_GIR_Drop(Convert.ToUInt16(0xA9AA), Convert.ToUInt16(j));  //new
                    i = true;
                    Msr_CA310_Vis();
                }
            }

        }
        void Write_GIR_Drop(UInt16 Addr, UInt16 date)//一组gamma写入
        {
            Send_buf = new byte[9];
            Setting.TempRecord.cmdLen = 9;
            Send_buf[0] = 0x3F;
            Send_buf[1] = 0x0E;
            Send_buf[2] = 4;
            //old  //先传高位再传低位
            //Send_buf[3] = Convert.ToByte(Addr >> 8 & 0xff);//地址高位
            ////Send_buf[4] = Convert.ToByte((date >> 6 & 0x0c) + 0x01);//0x01为u2所用  //这句貌似写错 先注释
            //Send_buf[4] = Convert.ToByte((date >> 8 & 0x0c));//数据低位

            //Send_buf[5] = Convert.ToByte(Addr & 0xff);  //地址低位
            //Send_buf[6] = Convert.ToByte(date & 0xff);  //数据低位

            Send_buf[3] = Convert.ToByte(Addr >> 8 & 0xff);
            Send_buf[4] = Convert.ToByte((date >> 8 & 0xff));
            Send_buf[5] = Convert.ToByte(Addr & 0xff);
            //Send_buf[6] = Convert.ToByte(date & 0x0c);
            Send_buf[6] = Convert.ToByte(date & 0x8f);

            CRCData.CalculateCrc16(Send_buf, out Send_buf[7], out Send_buf[8]);
            try { MainForm.comPort.Write(Send_buf, 0, 9); }
            catch { MessageBox.Show("串口有误，请检查", "错误"); }
        }

        private void ListBox3_BMPFile_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnPrevPic_Click(null, null);
            }
            else if (e.KeyCode == Keys.N)  // ,
            {
                OLED_Show_Click(btnPrevPic, null);
            }
            else if (e.KeyCode == Keys.M)
            {
                OLED_Show_Click(btnNextPic, null);
            }
        }

        private void BtnComConnect_Click(object sender, EventArgs e)
        {
            MainForm.btnOpenCom_Click(null, e);
        }

        private void BtnGrayShow_Click(object sender, EventArgs e)
        {
            MainForm.gray_level.Value = gray_level.Value;
            MainForm.gray_level_low.Value = gray_level_low.Value;
            MainForm.font_size = font_size;
            MainForm.BtnGrayShow_Click(null, e);
        }
        private void Gray_level_low_ValueChanged(object sender, EventArgs e)
        {
            if (gray_level_low.Value >= gray_level.Value)
            {
                if (gray_level.Value > 0)
                    gray_level_low.Value = gray_level.Value - 1;
                else
                {
                    gray_level_low.Value = 0;
                    gray_level.Value = 1;
                }
            }
        }
        string cmdstr = "";
        private int[] Point_692AXTest = new int[30] {//---副屏临时调试用绑点数据---
            255, 250,244,240,236,232,228,224,220,216,212,208,204,200,196,188,180,164,148,132,116,100,84,68,52,44,28,16,8,4 };
        private int[] Point_692AXDefault = new int[30] {//---Gamma默认绑点数据---
            255, 254, 252, 248, 244, 236, 228, 220, 212, 204, 196, 188, 180, 164, 148, 132, 116, 100, 84, 68, 52, 44, 36, 28, 20, 16, 12, 8, 4, 3 };
        private void ChksubDisTest_CheckedChanged(object sender, EventArgs e)
        {
            NumericUpDown[] NUMP = new NumericUpDown[] { RM692CX_30, RM692CX_29, RM692CX_28, RM692CX_27, RM692CX_26, RM692CX_25, RM692CX_24, RM692CX_23, RM692CX_22, RM692CX_21, RM692CX_20, RM692CX_19, RM692CX_18, RM692CX_17, RM692CX_16, RM692CX_15, RM692CX_14, RM692CX_13, RM692CX_12, RM692CX_11, RM692CX_10, RM692CX_9, RM692CX_8, RM692CX_7, RM692CX_6, RM692CX_5, RM692CX_4, RM692CX_3, RM692CX_2, RM692CX_1 };
            if (chksubDisTest.Checked)
            {
                for (int i = 0; i < 30; i++)
                {
                    NUMP[i].Value = Point_692AXTest[i];
                }
                //----------------------
                cmdstr = "FE,70;" +
                    "49,04;" + "4a,08;" + "4B,0F;" + "4c,1C;" + "4d,2C;" + "4e,34;" + "4f,44;" + "50,54;" + "51,64;" + "52,74;" +
                    "53,84;" + "54,94;" + "55,A4;" + "56,B4;" + "58,BC;" + "59,C4;" + "5a,C8;" + "5b,CC;" + "5c,D0;" + "5d,D4;" +
                    "5e,D8;" + "5f,DC;" + "60,E0;" + "61,E4;" + "62,E8;" + "63,EC;" + "64,F0;" + "65,F4;" + "66,FA;" + "67,ff;" +
                    "";//"DELAY,F0;FE,00";
                cmdstr = cmdstr.Replace(";", "\r\n");
                MainForm.txtProcessDrivercode(cmdstr, out string txtbaseClearOriginal, out string txtMClearOriginal);
                //MainForm.SendCmd(0);//低速写
                MainForm.SendCmd(4);//高速写

                textBox1.Text += "绑点已经恢复默认设置\r\n";
            }
            else
            {
                for (int i = 0; i < 30; i++)
                {
                    NUMP[i].Value = Point_692AXDefault[i];
                }
                //----------------------
                cmdstr = "FE,70;" +
                    "49,03;" + "4a,04;" + "4B,08;" + "4c,0C;" + "4d,10;" + "4e,14;" + "4f,1C;" + "50,24;" + "51,2C;" + "52,34;" +
                    "53,44;" + "54,54;" + "55,64;" + "56,74;" + "58,84;" + "59,94;" + "5a,A4;" + "5b,B4;" + "5c,BC;" + "5d,C4;" +
                    "5e,CC;" + "5f,D4;" + "60,DC;" + "61,E4;" + "62,EC;" + "63,F4;" + "64,F8;" + "65,FC;" + "66,FE;" + "67,FF;" +
                    "DELAY,F0;FE,00";
                cmdstr = cmdstr.Replace(";", "\r\n");
                MainForm.txtProcessDrivercode(cmdstr, out string txtbaseClearOriginal, out string txtMClearOriginal);
                //MainForm.SendCmd(0);//低速写
                MainForm.SendCmd(4);//高速写

                textBox1.Text += "绑点已经设置\r\n";
            }
        }

        private void BtnPreLoadBMP_Click(object sender, EventArgs e)
        {
            DirectoryInfo folder = new DirectoryInfo(textBox2.Text);

            foreach (FileInfo file in folder.GetFiles("*.bmp"))
            {

                Console.WriteLine(file.FullName);
                textBox1.Text += (file.FullName) + "\r\n";


                Int32 HACT_True = 0;
                Int32 VACT_True = 0;
                Int32 m_txlen = 0;
                Int32 j = 0;
                TimeNow2 = DateTime.Now; //开始计时

                byte[] TemFileContent = new byte[4096 * 3];
                System.Int32 ByteReaded = 0x00;
                try
                {
                    string FileName = (file.FullName);//textBox2.Text + '\\' + listBox3_BMPFile.SelectedItem.ToString();

                    FileStream aFile = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    ByteReaded = aFile.Read(TemFileContent, 0, 54);

                    HACT_True = (Convert.ToInt32(TemFileContent[19]) << 8) + TemFileContent[18];
                    VACT_True = Convert.ToInt32((TemFileContent[23] << 8) + TemFileContent[22]);
                    if (MainForm.HACT.Value != HACT_True)
                    {
                        textBox1.Text += "图片宽度与设定不符\r\n";
                        goto startshow;
                    }
                    j = 1;
                    {
                        aFile.Seek(54, SeekOrigin.Begin);
                        while (j <= VACT_True)
                        {
                            if (HACT_True % 4 != 0)
                                aFile.Seek((VACT_True - j) * (HACT_True * 3 + (HACT_True % 4)) + 54, SeekOrigin.Begin);
                            else
                                aFile.Seek((VACT_True - j) * HACT_True * 3 + 54, SeekOrigin.Begin);


                            ByteReaded = aFile.Read(TemFileContent, 0, HACT_True * 3);

                            try
                            {
                                MainForm.comPort.Write(TemFileContent, 0, ByteReaded);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                                break;
                            }
                            m_txlen += ByteReaded;
                            progressBar1.Value = Convert.ToInt32((double)m_txlen / aFile.Length * 100);
                            label14.Text = progressBar1.Value.ToString() + "%";

                            Application.DoEvents();
                            j++;
                        }

                        TemFileContent[0] = 0x5A;
                        try
                        {
                            MainForm.comPort.Write(TemFileContent, 0, 1);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }

                        aFile.Close();
                    }
                    /*
                    else
                    {
                        MessageBox.Show("BMP分辨率非法");
                    }*/
                }
                catch
                {
                    MessageBox.Show("请选择文件夹", "错误");
                }
            startshow: Thread.Sleep(100);
                TimeCount2 = DateTime.Now - TimeNow2;
                textBox1.Text = string.Format("{0:00}:{1:00}", TimeCount2.Seconds, TimeCount2.Milliseconds);

            }
        }

        private void rdoB4_CheckedChanged(object sender, EventArgs e)
        {
            btnIintFlash.Enabled = true;
        }

        private void Btncacrgb_Click(object sender, EventArgs e)
        {
            double Lr_d, Lg_d, Lb_d, r, g, b, t;

            D_x = Convert.ToDouble(txt2AX_X.Text);//取得预设的色坐标
            D_y = Convert.ToDouble(txt2AX_Y.Text);
            Lr_d = R_y * ((D_y - G_y) * (B_x - D_x) + (D_x - G_x) * (D_y - B_y)) / B_y / ((D_y - G_y) * (D_x - R_x) + (D_x - G_x) * (R_y - D_y));
            Lb_d = 1.0;
            Lg_d = G_y * ((D_y - R_y) * (B_x - D_x) + (D_x - R_x) * (D_y - B_y)) / B_y / ((D_y - R_y) * (D_x - G_x) + (D_x - R_x) * (G_y - D_y));
            t = Convert.ToDouble(txtinnit.Text);
            r = t * Lr_d / (Lr_d + Lg_d + Lb_d);
            g = t * Lg_d / (Lr_d + Lg_d + Lb_d);
            b = t * Lb_d / (Lr_d + Lg_d + Lb_d);
            labrgb.Text = t + "-" + "R:" + r.ToString("F4") + "  " + "G:" + g.ToString("F4") + "  " + "B:" + b.ToString("F4");
            textBox1.Text += labrgb.Text + "\r\n";

        }

        private void BtnSetRGBBase_Click(object sender, EventArgs e)
        {
            //RM692CX_X.Text = Convert.ToString(auto_x);
            //private double R_x = 0.6756;//
            //private double R_y = 0.3222;
            //private double G_x = 0.2129;
            //private double G_y = 0.7378;
            //private double B_x = 0.1412;
            //private double B_y = 0.0356;            
            //(auto_lum);

            OLED_Show_Click(btnred, e);
            Msr_CA310();
            Msr_CA310();
            R_x = auto_x;
            R_y = auto_y;
            textBox1.Text += "Set R_x：" + Convert.ToString(auto_x) + "\r\n";
            textBox1.Text += "Set R_y：" + Convert.ToString(auto_y) + "\r\n";

            OLED_Show_Click(btngreen, e);
            Msr_CA310();
            Msr_CA310();
            G_x = auto_x;
            G_y = auto_y;
            textBox1.Text += "Set G_x：" + Convert.ToString(auto_x) + "\r\n";
            textBox1.Text += "Set G_y：" + Convert.ToString(auto_y) + "\r\n";

            OLED_Show_Click(btnblue, e);
            Msr_CA310();
            Msr_CA310();
            B_x = auto_x;
            B_y = auto_y;
            textBox1.Text += "Set B_x：" + Convert.ToString(auto_x) + "\r\n";
            textBox1.Text += "Set B_y：" + Convert.ToString(auto_y) + "\r\n";
        }

        private void BtnData2DemuraRam_Click(object sender, EventArgs e)
        {
            PreLoad_Demura_data_Click();
            Flash_Write_Demura_data.Enabled = true;
        }

        private void R66451_btnPrevPic_Run(string filenames)       //R66451
        {
            Int32 i = 0;
            Int32 HACT_True = 0;
            Int32 VACT_True = 0;
            Int32 m_txlen = 0;
            Int32 j = 0;
            Int32 H_Size = 1134;
            byte[] TemFileContent = new byte[1024 * 100];
            byte[] TemFileContent1 = new byte[1024 * 100];
            System.Int32 ByteReaded = 0x00;
            try
            {
                //string FileName = textBox2.Text + '\' + listBox3_BMPFile.SelectedItem.ToString();

                string Filename;
                Filename = System.Windows.Forms.Application.StartupPath + "\\Resources\\M2\\" + filenames;//需见文件夹

                FileStream aFile = new FileStream(Filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                ByteReaded = aFile.Read(TemFileContent, 0, 54);
                HACT_True = (Convert.ToInt32(TemFileContent[19]) << 8) + TemFileContent[18];
                VACT_True = Convert.ToInt32((TemFileContent[23] << 8) + TemFileContent[22]);
                j = 1;
                //if ((HACT_True == ((TemFileContent[19] << 8) + (TemFileContent[18]))) && (Convert.ToInt32(VACT.Value) == (TemFileContent[23] << 8) + (TemFileContent[22])))//BMP图片水平大小
                {
                    aFile.Seek(54, SeekOrigin.Begin);
                    while (j <= VACT_True)
                    {
                        if (HACT_True % 4 != 0)
                            aFile.Seek((VACT_True - j) * (HACT_True * 3 + (HACT_True % 4)) + 54, SeekOrigin.Begin);
                        else
                            aFile.Seek((VACT_True - j) * HACT_True * 3 + 54, SeekOrigin.Begin);
                        ByteReaded = aFile.Read(TemFileContent, 0, HACT_True * 3); // 

                        try { MainForm.comPort.Write(TemFileContent, 0, ByteReaded); }
                        catch
                        { MessageBox.Show("串口有误，请检查", "错误"); break; }

                        m_txlen += ByteReaded;
                        progressBar1.Value = Convert.ToInt32((double)m_txlen / aFile.Length * 100);
                        label14.Text = progressBar1.Value.ToString() + "%";
                        Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                        j++;
                    }

                    TemFileContent[0] = 0x5A;
                    try
                    {
                        MainForm.comPort.Write(TemFileContent, 0, 1);
                    }
                    catch
                    {
                        MessageBox.Show("串口有误，请检查", "错误");
                    }

                    aFile.Close();
                }

            }
            catch
            {
                MessageBox.Show("请选择文件夹", "错误");
            }
        }
        private void GIR_Stop_Drop_Click(object sender, EventArgs e)
        {
            T20.Enabled = false;
            T19.Enabled = false;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;
            adjust_Rnum = 0;
            adjust_Gnum = 0;
            adjust_Bnum = 0;
            adjust_l = 0;
            adjust_x = 0;
            adjust_y = 0;
        }

        //LIR


        //Gamma调节一些点调不过，点此处跳过按钮,进下一个绑点测量
        //具体在此处做了小处理 if ((flag1 == 0 && flag2 == 0 && flag3 == 0) | (gammasetjumpflag))
        bool gammasetjumpflag = false;//jump 按钮
        private void gammasetjump_Click(object sender, EventArgs e)
        {
            gammasetjumpflag = true;
        }

        //暂停gamma调节，双击恢复可调
        private void btnGammasetPause_Click(object sender, EventArgs e)
        {
            if (T18.Tag == "pause")
            {
                T18.Enabled = true;
                T18.Tag = "";
                btnGammasetPause.Text = "Pause";
            }
            else
            {
                T18.Tag = "pause";
                T18.Enabled = false;
                btnGammasetPause.Text = "GOON";
            }
        }



    }
}
